Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the index of a loop in Applescript?

I have a list of different length strings that I'm looping:

set my_list to {"abc", "defg", "hi", "jklmno"}

repeat with the_item in my_list
    -- get index of the_item
end repeat

How can I find the index of the_item while I'm looping?

like image 434
ghickman Avatar asked Jun 07 '11 15:06

ghickman


1 Answers

You can either introduce a counter and update it with each iteration through the loop:

set counter to 0
repeat with the_item in my_list
  set counter to counter + 1
  -- do stuff
end repeat

or use a different loop form:

repeat with n from 1 to count of my_list
  -- do stuff with (item n of my_list)
end repeat
like image 131
Michael J. Barber Avatar answered Oct 22 '22 20:10

Michael J. Barber