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

Re: Position of an element in a list


  • Subject: Re: Position of an element in a list
  • From: Jim Underwood <email@hidden>
  • Date: Mon, 31 Jul 2017 21:42:43 +0000
  • Thread-topic: Position of an element in a list

Lots of native functions return either 0 or -1 for the index when the item is
not found.
It is easy and clear to branch on a value of 0.
I like that approach.  So to keep things simple and consistent, I use this:

________________________________

--- SIMPLE VERSION ---


set haystack to words of "alpha beta gamma delta epsilon zeta eta theta iota
kappa lambda mu"

set needle to "kappa"


set idx to my getIndex(needle, haystack)

-->10


set myList to {"TTT", 1, 67, "TT", 8}

set idx2 to my getIndex("TT", myList)

-->4


set listOfLists to {{"one", "two"}, {"three", "four"}, {"five", "six"}}

set idx3 to my getIndex({"three", "four"}, listOfLists)

-->2


if (idx > 0) then

  --- do something with idx

else

  --- throw error or notify user or other action

end if


on getIndex(pItem, pList)

  local idx, i



  set idx to 0

  set lenList to length of pList

  repeat with i from 1 to lenList

    if (pItem = (contents of item i of pList)) then

      set idx to i

      exit repeat

    end if

  end repeat



  return idx



end getIndex

________________________________



Best Regards,

Jim Underwood
aka JMichaelTX


From: AppleScript-Users
<applescript-users-bounces+jmichael=email@hidden<mailto:applescript-users-bounces+jmichael=email@hidden>>
 on behalf of Olivier MARTINEAU
<email@hidden<mailto:email@hidden>>
Date: Monday, July 31, 2017 at 10:54 AM
To: "ASUL (AppleScript)"
<email@hidden<mailto:email@hidden>>
Subject: Position of an element in a list


Hi everyone,


Just wanted to share some code.

I find it weird that I couldn't find any native function that would return the
position of a element in a list...

Maybe I overlooked something.

But I needed it, so I took the code of Emmanuel Levy and made it a bit more
fail-proof.

(in his code, position of "TT" in list {"TTT"} would give 1, which was not good
to me)



on positionInList(theItem, theList) -- credits Emmanuel Levy

set MARK to "◊" -- of course you won't be able to look for such character's
position in list, so choose one that isn't used

set ASTID to AppleScript's text item delimiters

set AppleScript's text item delimiters to MARK & return & MARK

set theList to MARK & theList & MARK

log theList

try

set theIndex to count paragraphs of text 1 thru (offset of (MARK & theItem &
MARK) in theList) of theList

on error

set theIndex to 0

end try

set AppleScript's text item delimiters to ASTID

return theIndex

end positionInList


set myList to {"TTT", 1, 67, "TT", 8}

my positionInList("TT", myList)


Enjoy !

😊


Olivier Martineau
Chef de projet DSI

Direct : 04 77 42 38 16
Mobile : 06 61 92 30 67
E-mail :
email@hidden<mailto:email@hidden>

(absent les mercredis et vendredis après-midi)

[http://gutenbergbaseline.gutenberg-networks.com/LogoGUNbaseline.jpg]<http://www.gutenberg-networks.com/>
34, rue Necker
42029 Saint-Étienne Cedex 1
www.gutenberg-networks.com<http://www.gutenberg-networks.com/>


-- Pensez à l'environnement, n'imprimez ce message que si nécessaire --


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • RE: Position of an element in a list
      • From: Olivier MARTINEAU <email@hidden>
References: 
 >Save of CSV Formatted File Fails on Excel 15.36 under Sierra (From: Thomas Maffucci <email@hidden>)
 >Re: Save of CSV Formatted File Fails on Excel 15.36 under Sierra (From: Yvan KOENIG <email@hidden>)
 >Re: Save of CSV Formatted File Fails on Excel 15.36 under Sierra (From: Shane Stanley <email@hidden>)
 >Position of an element in a list (From: Olivier MARTINEAU <email@hidden>)

  • Prev by Date: Re: Position of an element in a list
  • Next by Date: RE: Position of an element in a list
  • Previous by thread: Position of an element in a list
  • Next by thread: RE: Position of an element in a list
  • Index(es):
    • Date
    • Thread