BlakeOh Posted May 14, 2010 Share Posted May 14, 2010 Hi All, I'm new to AutoIt and loving it thus far. I have a (probably) very simple question for a script I am trying to create: What I'm doing is creating a simple GUI where a value is entered and the digits are added together and a new value is presented. What I cannot figure out how to do is add consecutive digits, for example: User enters 1234 User is presented with 10 (1+2+3+4) any help is appreciated! Link to comment Share on other sites More sharing options...
Malkey Posted May 14, 2010 Share Posted May 14, 2010 Here is an example. Local $value, $iIn, $sStr $iIn = InputBox("Testing", "Enter numbers") For $i = 1 To StringLen($iIn) $value += StringMid($iIn, $i, 1) $sStr &= StringMid($iIn, $i, 1) & "+" Next MsgBox(0, "Results", $value & " (" & StringTrimRight($sStr, 1) & ")") Link to comment Share on other sites More sharing options...
DW1 Posted May 14, 2010 Share Posted May 14, 2010 (edited) Another way to do the same thing, but with an array: #Include <Array.au3> $iIn = InputBox("Testing", "Enter numbers") $aString = StringSplit($iIn,"") MsgBox(0, "Results", Execute(_ArrayToString($aString,'+',1)) & " (" & _ArrayToString($aString,'+',1) & ")") Edited May 14, 2010 by danwilli AutoIt3 Online Help Link to comment Share on other sites More sharing options...
Marlo Posted May 14, 2010 Share Posted May 14, 2010 Dan beat me to it with the StringSplit method Dim $sNumbers = "123456789" ;Our numbers we want to add together. Dim $aNumbers = StringSplit($sNumbers, "") ;split te numbers up into an array so: [1] = 1, [2] = 2, etc Dim $iSum ;this variable will hold the answer For $I = 1 To $aNumbers[0] ;Go through each number in our array and add it to our sum $iSum += $aNumbers[$I] Next MsgBox(64, "Addition", $sNumbers & " added together is " & $iSum) ;Display the answer RickB75 1 Click here for the best AutoIt help possible.Currently Working on: Autoit RAT Link to comment Share on other sites More sharing options...
Spiff59 Posted May 14, 2010 Share Posted May 14, 2010 Another way to do the same thing, but with an array: #Include <Array.au3> $iIn = InputBox("Testing", "Enter numbers") $aString = StringSplit($iIn,"") MsgBox(0, "Results", Execute(_ArrayToString($aString,'+',1)) & " (" & _ArrayToString($aString,'+',1) & ")") Clever use of the _ArrayToString() delimiter and Execute(). Kudos. (also possibly the first useful implementation of Execute() I've seen in 2 years lol) Link to comment Share on other sites More sharing options...
Malkey Posted May 14, 2010 Share Posted May 14, 2010 Another example. Local $sStr = StringTrimRight(StringRegExpReplace(InputBox("Testing", "Enter numbers"), "(\d)", "\1+"), 1) MsgBox(0, "Results", Execute($sStr) & " (" & $sStr & ")") Link to comment Share on other sites More sharing options...
BlakeOh Posted May 15, 2010 Author Share Posted May 15, 2010 Thanks so much guys! What a great community. This is actually the first part of a larger project and I stumbled a bit. I will play around with all these examples. Thanks again all. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now