Re: Determining item number matching "x" in a list
Re: Determining item number matching "x" in a list
- Subject: Re: Determining item number matching "x" in a list
- From: Paul Skinner <email@hidden>
- Date: Wed, 12 Mar 2003 07:37:35 -0500
Here's a TID's-based method that only iterates over the instances of
the search term [1] and returns the index of each occurrence.
[1] Standard disclaimers apply:
More than 4000+- instances of the search term will break it.
caffeine is not yet fully metabolized.
ItemIndex({searchTerm:"c", inputList:{"c", "a", "a", "b", "c", "cc",
"c", "etc", "c", "c"}})
-->{1, 5, 7, 9, 10}
on ItemIndex(parameters)
set pd to AppleScript's text item delimiters
set rareDelimiter to "o#?"
set thisIndex to 0
set indices to {}
set AppleScript's text item delimiters to {""}
set searchTerm to rareDelimiter & searchTerm of parameters &
rareDelimiter
set inputList to inputList of parameters
set AppleScript's text item delimiters to rareDelimiter & rareDelimiter
set inputList to ({rareDelimiter} & inputList & {rareDelimiter}) as
list
set AppleScript's text item delimiters to {rareDelimiter &
rareDelimiter}
set inputList to inputList as text
set AppleScript's text item delimiters to {""}
set AppleScript's text item delimiters to searchTerm
set inputList to (every text item of inputList as list)
set occurances to (length of inputList) - 1
set previousItemsIndex to 0
set AppleScript's text item delimiters to {rareDelimiter &
rareDelimiter}
repeat with iteration from 1 to occurances
if item iteration of inputList is rareDelimiter & rareDelimiter then
set subItems to 1
else
if item iteration of inputList is "" then
set subItems to 0
else
copy (text 2 thru -2 of (item iteration of inputList)) to
thisSublist
set thisSublist to every text item of thisSublist
set subItems to (length of thisSublist)
end if
end if
set thisOccurancesIndex to previousItemsIndex + subItems
set the end of indices to thisOccurancesIndex
set previousItemsIndex to thisOccurancesIndex + 1
end repeat
set AppleScript's text item delimiters to pd
return indices
end ItemIndex
Paul Skinner
On Tuesday, March 11, 2003, at 03:10 PM, Steve Cunningham wrote:
I wonder if there is some trivial way to do this without writing a
repeat
loop that cycles through every value of the list looking for a match:
I have a list, theList = {"a","b","c","etc"} and I want to know the
item
number of the item that matches a variable x. I need something like
"get the item number of x in theList"
which returns a if x = "a", 2 if x = "b" etc.
Did I miss a page in the manual :-)
Thanks,
Steve
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.