Re: Get position of item in list
Re: Get position of item in list
- Subject: Re: Get position of item in list
- From: Kai <email@hidden>
- Date: Wed, 25 Jun 2003 01:46:12 +0100
on Tue, 24 Jun 2003 11:46:15 -0400, Paul Skinner wrote:
>
But that cannot possibly be the most efficient algorithm.*
>
This one will (like Kai's) uses the TIDs to find the item. It also
>
returns ALL of the occurrences of the search term.
>
>
>
set t to {}
>
repeat 5 times
>
repeat 1000 times
>
set the end of t to "abc"
>
end repeat
>
set the end of t to "lastItem"
>
end repeat
>
itemIndex({searchTerm:"lastItem", inputList:t}) -->{1001, 2002, 3003,
>
4004, 5005}
>
>
on itemIndex(parameters)
>
set rareDelimiter to ASCII character 240
>
set {prevTIDs, AppleScript's text item delimiters} to {AppleScript's
>
text item delimiters, {rareDelimiter & rareDelimiter}}
>
set indices to {}
>
set previousItemsIndex to 0
>
set inputList to ({rareDelimiter} & inputList of parameters &
>
{rareDelimiter}) as Unicode text
>
set AppleScript's text item delimiters to {rareDelimiter & searchTerm
>
of parameters & rareDelimiter}
>
set inputList to (text items of inputList)
>
set AppleScript's text item delimiters to {rareDelimiter &
>
rareDelimiter}
>
repeat with i from 1 to ((length of inputList) - 1)
>
if item i of inputList is rareDelimiter & rareDelimiter then
>
set subItems to 1
>
else
>
if item i of inputList is "" then
>
set subItems to 0
>
else
>
set subItems to (length of (text items of (text 2 thru -2 of
(item
>
i of inputList))))
>
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 prevTIDs
>
return indices
>
end itemIndex
This (faster) alternative is an all-occurrence variation on my original
suggestion:
==============================
property d : ASCII character 1
to getPosition of i from l
if i is not in l then return 0
set {n, o, t, text item delimiters} to {0, {}, text item delimiters, d & d}
set {l, text item delimiters} to {d & l & d, d & i & d}
set {l, text item delimiters} to {l's text items 1 thru -2, d & d}
repeat with i in l
set i to i as string
if i is not "" then set n to n + 1
tell n + (count i's text items) to set {n, o's end} to {it, it}
end repeat
set text item delimiters to t
o
end getPosition
set txtList to {"nine", "six", "six", "two", "sixteen", "sixty", "six"}
getPosition of "six" from txtList
--> {2, 3, 7}
==============================
--
Kai
_______________________________________________
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.