• 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: Offset in list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Offset in list


  • Subject: Re: Offset in list
  • From: Nigel Garvey <email@hidden>
  • Date: Fri, 11 Feb 2005 18:20:51 +0000

Bastiaan Boertien wrote on Fri, 11 Feb 2005 13:54:41 +0100:

>Hi List
>
>Is there a way to get the offset of an item from a list as number
>
>because we can do it with a string like this
>set theString to "658947123"
>
>offset of "4" in theString
>
>I know how to do it with a repeat but that's to slow

It depends what's in the list. If it's just integers and the one you want
is likely to be in the first 40 or so, a repeat compares quite favourably
with anything else for speed.

  on getIndex(theItem, theList)
    script o
      property l : theList
    end script

    if theList contains {theItem} then
      set i to 1
      repeat until (item i of o's l is theItem)
        set i to i + 1
      end repeat
    else
      set i to 0
    end if

    return i
  end getIndex

This is also a safe option if the list contains items that can't be
coerced to string or which might cause confusion if they were.

Otherwise, there's this slightly faster version of the handler jj posted
earlier:

  on IndexOfItem(theItem, theList)
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to return
    set theList to {"", theList, ""} as string
    set AppleScript's text item delimiters to {"", theItem, ""} as string
    set i to (count paragraphs of text item 1 of theList) mod (count
paragraphs of theList)
    set AppleScript's text item delimiters to astid

    return i
  end IndexOfItem


NG
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • numbers and prefix zero
      • From: Christian Vinaa <email@hidden>
    • Re: Offset in list
      • From: Paul Skinner <email@hidden>
  • Prev by Date: Re: where are the shell scripts ?
  • Next by Date: Excluded Dates in iCal Events
  • Previous by thread: Re: Slightly OT: InDesign cells \ tables
  • Next by thread: Re: Offset in list
  • Index(es):
    • Date
    • Thread