• 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: Paul Skinner <email@hidden>
  • Date: Fri, 11 Feb 2005 15:00:23 -0500

Want all the offsets? Nigel's comments apply. Delimiter conflicts are also a concern.

set myList to characters of "AAabcdefghABCDEFabcABCaAA"
set indexData to ItemIndex({"A", myList})
-->{occurances:6, indices:{1, 2, 11, 20, 24, 25}}


on ItemIndex(parameters)
set allPreceding to 0
set indices to {}
set AppleScript's text item delimiters to {""}
try
set searchTerm to ("" & (item 1 of parameters) as string) & ""
set AppleScript's text item delimiters to ""
--Add a buffer item at the beginning and end of dataToSearch to prevent the ends from matching.
set dataToSearch to (item 2 of parameters) as list
set dataToSearch to "" & (dataToSearch as text) & ""
on error --On a parameter error display parameter requirements.
set customErrorMessage to "parameters do not match declaration" & return & return & "required parameters:" & return & "{searchTerm, dataToSearch}"
DisplayErrorMessage({thisHandlersName, customErrorMessage, ""})
return
end try
--Find the searchTerm by breaking the dataToSearch list at each occurance.
set AppleScript's text item delimiters to searchTerm
set dataToSearch to (every text item of dataToSearch as list)
--If the dataToSearch doesn't break then searchTerm is not in the dataToSearch.
set occurances to (length of dataToSearch) - 1 --Two pieces = one break.
if occurances ≤ 0 then return {occurances:0, indices:{}}
--Count the pieces leading up to each break to get the index of each occurance.
set AppleScript's text item delimiters to {""} --Restore original list delimitation.
repeat with iteration from 1 to occurances
set thisSection to (every text item of (item iteration of (dataToSearch)))
if thisSection is not {""} then
set precedingItems to (count thisSection)
else
set precedingItems to 0
end if
set precedingItems to precedingItems + allPreceding --Item list position counter.
set thisIndex to precedingItems + 1
set the end of indices to thisIndex --List all indices.
set allPreceding to thisIndex
end repeat
set AppleScript's text item delimiters to {""}
return {occurances:occurances, indices:indices}
end ItemIndex


Paul Skinner

do shell script "osascript -e 'do shell script \"osascript -e beep\"'"
: )

On Feb 11, 2005, at 1:20 PM, Nigel Garvey wrote:

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:
email@hidden


This email sent to email@hidden


_______________________________________________ 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
References: 
 >Re: Offset in list (From: Nigel Garvey <email@hidden>)

  • Prev by Date: Excluded Dates in iCal Events
  • Next by Date: Re: QUOTED posix to HFS path
  • Previous by thread: Re: Offset in list
  • Next by thread: numbers and prefix zero
  • Index(es):
    • Date
    • Thread