• 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: kai <email@hidden>
  • Date: Sun, 13 Feb 2005 06:27:51 +0000


On Fri, 11 Feb 2005 15:00:23 -0500, Paul Skinner wrote:

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



This approach appears to be a bit faster:

--------------------

on itemIndices(i, l)
	set d to text item delimiters
	set text item delimiters to return & return
	set l to return & l & return
	set text item delimiters to return & i & return
	set l to l's text items
	set text item delimiters to d
	set c to 0
	set i to {}
	set o to (count l) - 1
	repeat with n from 1 to o
		set c to (count l's item n's paragraphs) div 2 + 1 + c
		set i's end to c
	end repeat
	{occurrences:o, indices:i}
end itemIndices

itemIndices("A", "AAabcdefghABCDEFabcABCaAA"'s items)
--> {occurrences:6, indices:{1, 2, 11, 20, 24, 25}}

--------------------

Similar comments to those made above by Paul apply. To operate on strings that include return characters, we'd obviously have to revert to using an unusual/unlikely* character or string for the separator - perhaps something like this:

--------------------

on itemIndices(i, l)
	set s to ASCII character 0
	set d to text item delimiters
	set text item delimiters to s & s
	set l to s & l & s
	set text item delimiters to s & i & s
	set l to l's text items
	set text item delimiters to s
	set c to 0
	set i to {}
	set o to (count l) - 1
	repeat with n from 1 to o
		set c to (count l's item n's text items) div 2 + 1 + c
		set i's end to c
	end repeat
	set text item delimiters to d
	{occurrences:o, indices:i}
end itemIndices

itemIndices("A", "AAabcdefghABCDEFabcABCaAA"'s items)
--> {occurrences:6, indices:{1, 2, 11, 20, 24, 25}}

--------------------

* On the subject of coming up with unusual/unlikely combinations, I'm reminded of an exchange some time back between a couple of our leading list luminaries. (The thread in question was "Getting label from a record"):

http://lists.apple.com/archives/applescript-users/2003/Nov/msg00578.html

;-)

---
kai
_______________________________________________
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:
    • Re: Offset in list
      • From: Paul Skinner <email@hidden>
  • Prev by Date: Re: Frontmost App
  • Next by Date: Re: Slightly OT: InDesign cells \ tables
  • Previous by thread: Re: numbers and prefix zero
  • Next by thread: Re: Offset in list
  • Index(es):
    • Date
    • Thread