• 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: Finding the Position of an item in a list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding the Position of an item in a list


  • Subject: Re: Finding the Position of an item in a list
  • From: Arthur J Knapp <email@hidden>
  • Date: Mon, 08 Oct 2001 15:22:54 -0400

> Date: Sun, 07 Oct 2001 12:57:39 -0400
> Subject: Finding the Position of an item in a list
> From: Greg Back <email@hidden>

> Is there any more practical (read "shorter") way to find where in a list a
> certain item can be found.
>
> to findPosition(thisItem, thislist)
> if thisItem is in thislist then
> repeat with i from 1 to (count thislist)
> if item i of thislist is thisItem then
> set j to i
> exit repeat
> end if
> end repeat
> else
> set j to "Not in list"
> end if
> return j
> end findPosition

You might find something in here more to your liking:

-- Simple and short repeat loop:
--
on GetIndex(a, i)
if a contains i then repeat with x from 1 to a's length
if a's item x = i then return x
end repeat
return 0
end GetIndex


-- Binary-Search, split the list into smaller parts, makes great use
-- of the "contains" operator.
--
to BinarySearch(a, i, l, r)
if a's items l thru r contains i then
repeat until l = r
set m to (r + l) div 2
if a's items l thru m contains i then
set r to m
else
set l to m + 1
end if
end repeat
else
return 0
end if
end BinarySearch

set the_list to {"a", "b", "c"}
BinarySearch(the_list, "b", 1, the_list's length)


-- String-Coercable-Items Technique, (case-sensitive):
--
set the_list to {"abc", 123, true, "def"} -- all items coercable to a string
offsetOf(the_list, true)

property offsetSentinal : ASCII character 1 -- speed up repeated calls

on offsetOf(a, s) -- case-sensitive
set oldDelim to text item delimiters
set text item delimiters to offsetSentinal
set a to offsetSentinal & a & offsetSentinal
set x to offset of (offsetSentinal & s & offsetSentinal) in a
if (x is not 0) then
set x to count of text items in (a's text 1 thru x)
set x to x - 1
end if
set text item delimiters to oldDelim
return x
end offsetOf



Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden

Hey, check out:
http://www.zavatone.com/


  • Prev by Date: RE: Renaming a folder
  • Next by Date: Re: Project Builder & AppleScript
  • Previous by thread: Re: Finding the Position of an item in a list
  • Next by thread: Image Capture Scripting - what is it doing to my images?
  • Index(es):
    • Date
    • Thread