error471 Posted January 28, 2016 Share Posted January 28, 2016 (edited) Hey folks, I have a simple problem with an array sum. The pictures show what my intention is: Every digit in the row should be summed into a new column at the rightmost of the array. Hope the problem is not new and someone knows a solution. Best wishes. error471 Edited January 28, 2016 by error471 Link to comment Share on other sites More sharing options...
kylomas Posted January 28, 2016 Share Posted January 28, 2016 (edited) error471, Try this... #include <array.au3> local $aArray = [ _ ['Alex',1,2,3], _ ['Bob',0,0,9,5,' '], _ ['Ted',10,0,9,5,17] _ ] redim $aArray[ubound($aArray)][ubound($aArray,2) + 1] local $tot = 0 for $i = 0 to ubound($aArray) - 1 for $j = 0 to ubound($aArray,2) - 1 if IsNumber($aArray[$i][$j]) then $tot += $aArray[$i][$j] next $aArray[$i][ubound($aArray,2) - 1] = $tot $tot = 0 next _arraydisplay($aArray) kylomas Edited January 28, 2016 by kylomas corrected code GoogleGonnaSaveUs 1 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
error471 Posted January 28, 2016 Author Share Posted January 28, 2016 @kylomas: Your script works, but I can't integrate it into my project. Weird... Seems to make difficulties with this line: IsNumber($aLVArray[$i][$j]) then $tot = $tot + $aLVArray[$i][$j] Link to comment Share on other sites More sharing options...
Jfish Posted January 28, 2016 Share Posted January 28, 2016 Any chance your source array is being read from a spreadsheet? Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
kylomas Posted January 28, 2016 Share Posted January 28, 2016 error message? Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
error471 Posted January 28, 2016 Author Share Posted January 28, 2016 I found the problem. I changed this: IsNumber($aLVArray[$i][$j]) then $tot = $tot + $aLVArray[$i][$j] to that: $tot = $tot + $aLVArray[$i][$j] It works now, but checking for digits would still be very crucial... Link to comment Share on other sites More sharing options...
kylomas Posted January 28, 2016 Share Posted January 28, 2016 This stmt should work if IsNumber($aArray[$i][$j]) then $tot += $aArray[$i][$j] can you post a reproducer? kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
error471 Posted January 28, 2016 Author Share Posted January 28, 2016 Here it is: #include <array.au3> #include <File.au3> Global $aArray _FileReadToArray("array.txt", $aArray,0,"|") _arraydisplay($aArray) redim $aArray[ubound($aArray)][ubound($aArray,2) + 1] local $tot = 0 for $i = 0 to ubound($aArray) - 1 for $j = 0 to ubound($aArray,2) - 1 if IsNumber($aArray[$i][$j]) then $tot += $aArray[$i][$j] next $aArray[$i][ubound($aArray,2) - 2] = $tot $tot = 0 next _arraydisplay($aArray) array.txt Link to comment Share on other sites More sharing options...
kylomas Posted January 29, 2016 Share Posted January 29, 2016 error471, Two things... 1. You did not pick up the edited code I posted. This $aArray[$i][ubound($aArray,2) - 2] = $tot was changed to $aArray[$i][ubound($aArray,2) - 1] = $tot 2. Your data is really a string so I changed the function IsNumber to StringIsDigit. It should work like this... #include <array.au3> #include <File.au3> Global $aArray _FileReadToArray("array.txt", $aArray,0,"|") _arraydisplay($aArray) redim $aArray[ubound($aArray)][ubound($aArray,2) + 1] local $tot = 0 for $i = 1 to ubound($aArray) - 1 for $j = 0 to ubound($aArray,2) - 1 if stringisdigit($aArray[$i][$j]) then $tot += $aArray[$i][$j] next $aArray[$i][ubound($aArray,2) - 1] = $tot $tot = 0 next _arraydisplay($aArray) kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
error471 Posted January 29, 2016 Author Share Posted January 29, 2016 @kylomas: It works. Thank you! Link to comment Share on other sites More sharing options...
iamtheky Posted January 29, 2016 Share Posted January 29, 2016 shrunk a bit #include <File.au3> Global $aArray _FileReadToArray("array.txt", $aArray,0,"|") _arraydisplay($aArray) redim $aArray[ubound($aArray)][ubound($aArray,2) + 1] for $i = 1 to ubound($aArray) - 1 $aArray[$i][ubound($aArray,2) - 1] = execute(stringtrimright(_ArrayToString($aArray , "+" , $i , $i , @CRLF , 1) , 1)) next _arraydisplay($aArray) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) 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