Page 1 of 1

Arrays

Posted: Thu Sep 04, 2014 3:44 pm
by paulrichards999
I have read many documents for creating arrays in livecode, and some examples are good, just not quite what i'm looking for. I'm trully stuck now :(

lets say I have a CSV file containing 2 items of info per line
(qty & description)
1,Cottage Pie
1,Nut Roast
1,Cottage Pie
1,Veg Stew
1,Cottage Pie

How would i trawl through each line and place into an array and then display the info, so the result would read
3 Cottage Pie
1 Nut Roast
1 Veg Stew

Any help suggestions would be greatly received.. Thanks :D

Re: Arrays

Posted: Thu Sep 04, 2014 4:21 pm
by Klaus
Hi Paul,

here is a cool trick:

Code: Select all

...
put empty into tArray
repeat for each line tLine in tCSVData
  put item 1 of tLine into tPrice
  put item 2 of tLine into tItem
  add tPrice to tArray[tItem]
end repeat
...
Will create an array with the description as key names and the sum of the qty as the content:
tArray["Cottage Pie"] -> 3


Best

Klaus

Re: Arrays

Posted: Thu Sep 04, 2014 4:35 pm
by sefrojones
Looks like Klaus beat me to it, but you can add this bit to answer/put/whatever your finished list:

Code: Select all

   repeat for each key tKey in tArray
      put tArray[tkey]&&tkey &&cr after myResults
   end repeat
   sort myResults descending
   answer myresults

--Sefro

Re: Arrays

Posted: Thu Sep 04, 2014 5:00 pm
by paulrichards999
Thank you so much Sefro & klaus... You have helped me massively

Re: Arrays

Posted: Thu Sep 04, 2014 6:30 pm
by dunbarx
Here as another example. With all these instances you should be on your way with arrays. On a new card, make a field with this in its text:
aaa bbb ccc aaa bbb ccc ddd rrr aaa bbb

Make a button. Put this into its script:

Code: Select all

on mouseUp
   get fld 1
   repeat for each word tWord in it
      add 1 to wordCount[tword]
   end repeat
   combine wordCount by return and comma
   answer wordCount
end mouseUp
Step through the handler. Make sure you expand the array in the debugger. See how the array is updated each pass through the loop?

Craig Newman

Re: Arrays

Posted: Thu Sep 04, 2014 7:02 pm
by paulrichards999
Another fantastic example, thanks Craig... All becoming much much clearer now.