Arrays

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
paulrichards999
Posts: 19
Joined: Thu Sep 04, 2014 3:32 pm

Arrays

Post by paulrichards999 » Thu Sep 04, 2014 3:44 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Arrays

Post by Klaus » Thu Sep 04, 2014 4:21 pm

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

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Arrays

Post by sefrojones » Thu Sep 04, 2014 4:35 pm

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

paulrichards999
Posts: 19
Joined: Thu Sep 04, 2014 3:32 pm

Re: Arrays

Post by paulrichards999 » Thu Sep 04, 2014 5:00 pm

Thank you so much Sefro & klaus... You have helped me massively

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Arrays

Post by dunbarx » Thu Sep 04, 2014 6:30 pm

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

paulrichards999
Posts: 19
Joined: Thu Sep 04, 2014 3:32 pm

Re: Arrays

Post by paulrichards999 » Thu Sep 04, 2014 7:02 pm

Another fantastic example, thanks Craig... All becoming much much clearer now.

Post Reply