[How To] Iterate List delimited by something

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
palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

[How To] Iterate List delimited by something

Post by palanolho » Mon Oct 21, 2013 12:27 pm

Greetings everyone.

I know this is noob question with a simple answer, but I have been looking for the answer and have not yet found it, so, If someone could help me, I really appreciate it.

I need to go through a list of item delimited by commas and for each item, do something with it

I know I can use the loops for that (Repeat-until, do-while, etc...) but my problem is handling the list.

The number of items in the list may be different each time the handler is called and it may look like this: "aaa,555,bb22,a b c d,1 2 3 4"

What can I do to get each element of the list?


If I use a repeat until loop (or something like that) with something like:

Code: Select all

put item i of myList into tVar

how can I know that I'm on the last item of the list OR that there are no more item on the list?

If the list delimiter is different, can I use the set the itemDelimiter to ":" to change it?

Many thanks, and sorry for this lame question ...
- Miguel
Last edited by palanolho on Mon Oct 21, 2013 12:52 pm, edited 1 time in total.

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

Re: [How To] Iterate List delimited by something

Post by Klaus » Mon Oct 21, 2013 12:45 pm

HHola Miguel,

you can use ANY (single) character as an ITEMDELIMITER, whatever fits your list :D

You can "loop" thorugh your list in two ways:

Code: Select all

...
repeat with i = 1 to teh nium of items of your_item_list
 ### do something with -> item i of your_item_list
end repeat
...
Or even faster:

Code: Select all

...
repeat for each item tItem in your_item_list
  ## do something with tItem
end repeat
...
Hint: With "repeat fore each..." the items/lines can not be modified like with "repeat with i = ..."


Best

Klaus

palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

Re: [How To] Iterate List delimited by something

Post by palanolho » Mon Oct 21, 2013 12:50 pm

thank you!

I know it would be someth9ng that simple.... Why don't the LC dictionary have such simple examples...

Thanks alot
- Miguel

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

Re: [How To] Iterate List delimited by something

Post by dunbarx » Mon Oct 21, 2013 2:51 pm

Hi,

What Klaus really was implying is that LC can use a property to determine the number of items in your list, regardless of how many there are, and regardless of whether that number changes the next time you run the very same code. This is a powerful feature of the language, and can be explicitly tested:

answer the number of items of yourList

If you think about this, you will see that it is embedded in the two examples Klaus gave you. Saves a whole lot of worrying about the nature of your list.

Craig Newman

Post Reply