Loop thru an array

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

Loop thru an array

Post by dantomlin » Thu Jul 22, 2010 3:46 pm

I have data in a datagrid and an item number may appear more than once for a particular order. This is because when the order shipped the item was shipped in multiple boxes and therefore each line represents the qty shipped for that "box" for that item.

For example
Order # Item # Qty Shipped
123 456 24
123 456 24
123 789 12
124 456 10

I need to update a database with this information but I need to update Order 123 for item 456 the qty of 48, then update order 123 for item 789 the qty of 12, then update order 124 for item 456 the qty of 10.

How do I loop thru the array or tab-delimited data to summarize the qty? Which is better to use an array or tab-delimited list?

Thanks, Dan

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

Re: Loop thru an array

Post by dunbarx » Thu Jul 22, 2010 6:39 pm

Hi.

I am still new to arrays, but this seems ideally suited for one. If you have your data in a field, say, then in a button script put

Code: Select all

on mouseUp
   put fld "yourField" into temp
   repeat with y = 1 to the number of lines of temp
      put word 1 of line y of temp & numtoChar(235) & word 2 of line y of temp && word 3 of line y of temp into line y of temp
   end repeat
   
   repeat with y = 1 to the number of lines of temp
      add word 2 of line y of temp to accum[word 1 of line y of temp]
   end repeat
   
   combine accum using return and space
   
   replace numtoChar(235) with space in accum
   answer accum
end mouseUp
I bet this can be shortened, but I just threw it together. I assumed you parsed your data as per your example, that is, with spaces.

Craig Newman
Last edited by dunbarx on Thu Jul 22, 2010 7:29 pm, edited 2 times in total.

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

Re: Loop thru an array

Post by dantomlin » Thu Jul 22, 2010 7:07 pm

Thanks, it worked great....

Post Reply