Re: Offset in list
Re: Offset in list
- Subject: Re: Offset in list
- From: Paul Skinner <email@hidden>
- Date: Mon, 14 Feb 2005 09:14:13 -0500
Thanks for condensing that Kai! I saw the trees, but the forest eluded
me.
As for avoiding TIDs collisions, how about this? Do you see any method
to do this that doesn't duplicate data and without using 'contains'? On
large data sets both of these are limiting factors.
on itemIndices(i, l)
set d to text item delimiters
set AppleScript's text item delimiters to ""
set listAsText to l as text
repeat with c from 0 to 255
set s to ASCII character c
set text item delimiters to s
if (length of text items of listAsText) is 1 then exit repeat
end repeat
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", "A A abcdefghABC DEFabcABCaAA"'s items)
--> {occurrences:6, indices:{1, 2, 11, 20, 24, 25}}
Paul Skinner
There are no absolutes.
On Feb 13, 2005, at 1:27 AM, kai wrote:
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:
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