Find item in list and return item number

I need help! I would like to return the item number in a list.

For instance, I would like to search for “milk” in the following list:

{“bread”, “milk”, “butter”, “eggs”}

Since “milk” is the second item in the list, I would like the script to return 2.

Thanks,
Tom

This handler from Apple should fill the bill.

set foo to {"bread", "milk", "butter", "eggs"}
set item_num to my list_position("milk", foo)

on list_position(this_item, this_list)
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then return i
	end repeat
	return 0
end list_position

– Rob

It worked perfectly. Thanks for the help!!!

Also helped me years later…thank you.

Hello

I found this little beauty by Emmanuel Levy some months later hidden in this thread.
This one should prove a lot faster, but not tested.


on indexof(theItem, theList) -- credits Emmanuel Levy
	set text item delimiters to return
	set theList to return & theList & return
	set text item delimiters to {""}
	try
		-1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
	on error
		0
	end try
end indexof

Best Regards

McUsr

I feel that it would be safer to edit it this way :


on indexof(theItem, theList) -- credits Emmanuel Levy
	set oTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to return
	set theList to return & theList & return
	set AppleScript's text item delimiters to oTIDs
	try
		-1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
	on error
		0
	end try
end indexof

After all,the in use delimiters may be different than {“”}

Yvan KOENIG (VALLAURIS, France) dimanche 4 juillet 2010 12:14:00

sorry to post private things. Yvan, did you get my message, your email account seems to be broken

Hello.

@Yvan:

Stefan pointed out for me the other day, that you don’t have to use AppleScript’s text item delimiters when you have an application in its scope. (No surrounding tell block).

-I’m living in the belief that these text item delimiters is local to the block they are used.

It is very timely with info on this, because I have a beast for you guys hopefully today, I’m working on the last buggy behavior. :slight_smile:
Edit: It is a beast internally, but is really easy to use from your code, and doesn’t obstract your code’s logic much, considering the useful things it does. It was made to give your scripts added functionality transparently to your original code. I shall say no more but Try it! when I have uploaded it to Code Exchange. It comes with a fully working Demo.

Best Regards

McUsr

Hello mcUsr

the important part wasn’t AppleScript’s but oTIDs :wink:

Yvan KOENIG (VALLAURIS, France) dimanche 4 juillet 2010 15:49:05

Hello stephanK

I received nothing.
I learnt this morning that my provider had some problems in the period 2010/07/01 . 2010/07/03

A friend trieng to send me a mail deceived :

: delivery temporarily suspended: host
smtp-in.sfr.fr[93.17.128.7] refused to talk to me: 421 4.7.0
msfrf2102.sfr.fr Error: too many connections from 80.12.242.144

At this time it seems to be OK.

I may bve rude but I’m polite enough to respond when some one send me a mail (mainly when it’s a response to a request of mine).

Yvan KOENIG (VALLAURIS, France) dimanche 4 juillet 2010 15:55:24

OK, but the problem occurred on June, 26.
I’ve just sent you the message again

Hello

By all means, thanks!

Best Regards

McUsr