• 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: Nigel Garvey <email@hidden>
  • Date: Tue, 24 Jun 2003 15:59:04 +0100

Emmanuel wrote on Tue, 24 Jun 2003 10:14:52 +0200:

>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

That was the EL version, as I remember. The NGEL version was:

on NGELItemIndexOfString(theItem, theList)
set astid to text item delimiters
set text item delimiters to return
set theList to return & theList & return
set text item delimiters to return & theItem & return
if (count theList's text items) > 1 then
set theIndex to (count paragraphs of text item 1 of theList)
else
set theIndex to 0
end if
set text item delimiters to astid
return theIndex
end NGELItemIndexOfString

The 'if' block here can be replaced with:

set theIndex to (count paragraphs of text item 1 of theList) mod (count
paragraphs of theList)

... which seems to be slightly faster when theItem's in theList, slightly
slower when it's not. I haven't tested it with long lists though. It may
be slower anyway then.

NG
_______________________________________________
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.

  • Prev by Date: Open file using Creator
  • Next by Date: Summary: display dialog
  • Previous by thread: Re: Get position of item in list
  • Next by thread: Re: Get position of item in list
  • Index(es):
    • Date
    • Thread