Re: Index from list
Re: Index from list
- Subject: Re: Index from list
- From: "Arthur J Knapp" <email@hidden>
- Date: Mon, 19 Feb 2001 14:17:58 -0500
>
Date: Mon, 19 Feb 2001 12:01:15 +0000
>
From: Pier Kuipers <email@hidden>
>
Subject: Index from list
>
I am trying to find a "clean and quick" way to find out the index of
>
any item in an unordered list with Vanilla Applescript.
Try some of these:
set aList to {"abc", 3.14159, true, "def", "ghi"}
indexOf(aList, 3.14159)
-- > 2
indexOfBigList(aList, "def", 1, length of aList)
-- > 4
set StringList to {"abc", "d", "ef", "g"}
indexOfStringList(StringList, "d")
-- > 2
on indexOf(lst, itm)
if (lst contains {itm}) then
set x to 1
repeat until (item x of lst = itm)
set x to x + 1
end repeat
return x
else
return 0
end if
end indexOf
on indexOfBigList(lst, itm, l, r) -- big lists
set m to ((r - l) div 2) + l
if (items l thru m of lst contains {itm}) then
if (l = m) then
return l
else
return indexOfBigList(lst, itm, l, m)
end if
else if (items (m + 1) thru r of lst contains {itm}) then
if (m + 1 = r) then
return r
else
return indexOfBigList(lst, itm, m + 1, r)
end if
else
return 0
end if
end indexOfBigList
on indexOfStringList(lst, str) -- big list of strings
-- every item of lst must be string
-- str must be case sensitive
considering case
if (lst does not contain {str}) then
return 0
end if
end considering
-- else
set delim to ASCII character 4 -- something unusual
set text item delimiters to delim
set {lst, text item delimiters} to {delim & (lst as string), delim &
str}
set {lst, text item delimiters} to {text items of lst, delim}
set {x, text item delimiters} to {count text items in (item 1 of lst),
{""}}
return x
end indexOfStringList
--
{
Arthur J Knapp, of STELLARViSIONs ;
http://www.STELLARViSIONs.com ;
mailto:email@hidden ;
"...well the rain falls down
without my help, I'm afraid
and my lawn gets wet,
though I withheld my consent..."
}