• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Get position of item in list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get position of item in list


  • Subject: Re: Get position of item in list
  • From: Paul Skinner <email@hidden>
  • Date: Tue, 24 Jun 2003 11:46:15 -0400

On Tuesday, June 24, 2003, at 04:14 AM, Emmanuel wrote:

At 4:51 AM +0100 24/06/03, Kai wrote:
on Mon, 23 Jun 2003 14:12:32 -0500, Joseph Weaks <email@hidden> wrote:

I know how to do this with a repeat loop, but surely there's a syntax I
can't figure out for

property: myList: {"foo","bar"}

set foobar to the position of "bar" in myList

'Fraid not, Joe. If your list is a short one, stick with the repeat loop.
For long lists, you might try a tid-based approach:

Once there was a thread on that topic and if I remember correctly the most efficient algorithm was found to be the following.

-------------------------------------------
on NGELItemIndexOfString(theString, theList)
set theList to return & theList & return
try
-1 + (count paragraphs of (text 1 thru (offset of (return & theString & return) in theList) of theList))
on error
return 0
end try
end NGELItemIndexOfString
-------------------------------------------

NGELItemIndexOfString("too", {"wan", "too", "tree", "for"})
-- 2

Emmanuel


TIDs bite again!
I think you forgot to point out that yours were set to return when you ran this...

on NGELItemIndexOfString(theString, theList)
set AppleScript's text item delimiters to return
set theList to return & theList & return
try
-1 + (count paragraphs of (text 1 thru (offset of (return & theString & return) in theList) of theList))
on error
return 0
end try
end NGELItemIndexOfString

...works better for me.

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

Paul Skinner

* No time to test. Besides... Perl.
_______________________________________________
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.

References: 
 >Re: Get position of item in list (From: Emmanuel <email@hidden>)

  • Prev by Date: Summary: display dialog
  • Next by Date: Re: Applescript, Panther, and X-Code
  • Previous by thread: Re: Get position of item in list
  • Next by thread: Re: Get position of item in list
  • Index(es):
    • Date
    • Thread