Jump to content
error471

Sum all digits within a mixed array.

Recommended Posts

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

Array-Example.png

Array-Example-Wish.png

Edited by error471

Share this post


Link to post
Share on other sites

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 by kylomas
corrected code

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

Share this post


Link to post
Share on other sites

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...

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...