Re: How to get the index of an item in a list
Re: How to get the index of an item in a list
- Subject: Re: How to get the index of an item in a list
- From: "Serge Belleudy-d'Espinose" <email@hidden>
- Date: Mon, 25 Jun 2001 11:13:24 +0200
At 2:45 -0500 25/06/01, Greg Strange wrote:
>
I can see where I could loop through with a counter and get it but isn't
>
there a better way?
If your list is only strings, you can use TIDs. Here is a simple handler:
>
on _list_offset(xItem, xList)
>
>
set xLength to xList's length
>
set xTID to "*"
>
>
set {oldTID, AppleScript's text item delimiters} to ,
>
{AppleScript's text item delimiters, xTID}
>
>
set xList to xTID & xList & xTID
>
>
set AppleScript's text item delimiters to xTID & xItem & xTID
>
>
set xList to xList's text items
>
>
set AppleScript's text item delimiters to xTID
>
>
set xOffset to (xList's first item's text items)'s length
>
>
set AppleScript's text item delimiters to oldTID
>
>
if xOffset > xLength then set xOffset to 0
>
>
return xOffset
>
>
end _list_offset
>
>
_list_offset("un", {"un", "deux", "trois"})
>
--> 1
>
_list_offset("trois", {"un", "deux", "trois"})
>
--> 3
>
_list_offset("quatre", {"un", "deux", "trois"})
>
--> 0
The main drawback of this handler is that TIDs are case sensitive:
>
_list_offset("Un", {"un", "deux", "trois"})
>
--> 0
Serge
--
\\//\//\// Serge Belleudy-d'Espinose Institut Jacques Monod
// // //
http://www.ijm.jussieu.fr/ Univ. Jussieu - Paris
//\//\//\\