Sub total a list

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dantomlin
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue Feb 26, 2008 4:07 pm

Sub total a list

Post by dantomlin »

I have a list of the following data:

CounterKicks, 1000
CounterKicks, 900
BallisLife, 1200
BallisLife, 500
HoopMix, 1000

I need to write a loop so I can get a sub-total for each item (word 1 of the list).
My result s/b:
Counterkicks, 1900
Ballislife, 1700
HoopMix, 1000

I can get the loop to put 3 lines in my result with the correct names but I can't seem to get the amounts to add up correctly.

Any help would be greatly appreciated.... :)
sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Sub total a list

Post by sturgis »

Probably the easiest way to do this would be with an array. If the data is in a variable say sData you can use the first item as the array key and then just keep adding as you go through the items.

Code: Select all

repeat for each line tLine in sData -- go through all the lines of sdata
 add item 2 of tLine to sDataA[item 1 of tLine]
end repeat
This will create an array similar to the following.

sDataA["CounterKicks"] has the value of 1900
sDataA["BallisLife"] has the value of 1700
sdataA["HoopMix"] is of course 1000
dantomlin
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue Feb 26, 2008 4:07 pm

Re: Sub total a list

Post by dantomlin »

Thanks... that worked... 8)
NoN'
Posts: 96
Joined: Thu Jul 03, 2008 9:56 pm
Contact:

Re: Sub total a list

Post by NoN' »

Hi,
An exemple of implementation of sub total realised with HC/Livecode can be find here :

http://rg75.free.fr/hyperespace.T_AlK.fr.htm

It is probably the easiest way to do this kind of thing.
Only in french, for the moment, but the english version should come soon.

Regards,

Renaud
Post Reply