Returning Indicies of an Array

I did a search on osaxen and didn’t seem to find what I was looking for… Does anyone know of a SA that lets you return indexes rather than values from an array?

You want something like this?

set myArray to {"one", 2, "three", "four"}
getIndex(myArray, "three")

to getIndex(mylist, val)
	if val is in mylist then repeat with k from 1 to count mylist
		tell item k of mylist to if it = val then return k
	end repeat
end getIndex

Exactly, but without having to do a repeat through it =)

I know the idea has been mentioned to Sal on many occasions. It’s frustrating that since you can refer to list items by their index you can’t natively get back an index from a choose statment for instance.

So is I have

set theList to {"word 1", "word 2", "word 3"}
choose from list theList

and someone chooses “word 2” it would bre great to be returned back 1… or rather 2 since the first Index in AS is 1 not 0. Better yet having a choice would be nice

set valReturned to choose from list theList

--versus something like

set idxRetuened to index returned of choose from list theList

Hi James,

in some of my scripts I used a workaroud like this (sorry for the stupid example …):

set theFolder to choose folder
set theMenu to {}
set n to 1

tell application "Finder"
	set folderItems to (items of theFolder)
	repeat with theItem in folderItems
		set theMenu to theMenu & (my leadingZeros(n) & " - " & name of theItem)
		set n to n + 1
	end repeat
end tell

set theSelection to (characters 1 thru 3 of item 1 of (choose from list theMenu) as string) as integer
tell application "Finder"
	set thePath to POSIX path of (item theSelection of folderItems as string)
end tell
display dialog "the path of your selected file is " & thePath

on leadingZeros(n)
	if n > 99 then
		return n as string
	else if n > 9 then
		return "0" & n
		return "00" & n
	end if
end leadingZeros