• 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: Thu, 3 Aug 2017 20:51:46 +0000
  • Thread-topic: Position of an element in a list

Shane and Nigel,

I started out to do some timing tests on the various methods for this.  But
after I ran the first test using Nigel's 5000-item list with my handler, on
getIndex(pItem, pList),  I stopped testing. My handler found the item in 0.767
sec.  At this point I don't see a practical reason to use anything else, unless
I have a very, very large list (>> 5,000).  But maybe I'm missing something?

Running Script Editor 2.8.1 (183.1) on macOS 10.11.6.
Run on iMac
27-Inch<http://www.everymac.com/systems/apple/imac/specs/imac-core-i5-3.3-27-inch-aluminum-retina-5k-late-2015-specs.html>
 Late
2015,<http://www.everymac.com/systems/apple/imac/specs/imac-core-i5-3.3-27-inch-aluminum-retina-5k-late-2015-specs.html>

MK482LL/A<http://www.everymac.com/systems/apple/imac/specs/imac-core-i5-3.3-27-inch-aluminum-retina-5k-late-2015-specs.html>,Quad
 Core 3.3 i5, 24GB, Retina 5K,
<http://www.everymac.com/systems/apple/imac/specs/imac-core-i5-3.3-27-inch-aluminum-retina-5k-late-2015-specs.html>
 2TB Fusion Drive
________________________________

Time to Find Item in 5000-Item List


(*START: Thursday, August 3, 2017 at 3:26:32 PM*)

(*JM getIndex:

    • Thursday, August 3, 2017 at 3:26:32 PM

    • EXECUTION TIME: 0.767 sec*)

(*idxJM: 5000*)

(*STOP:

    • Thursday, August 3, 2017 at 3:26:32 PM

    • EXECUTION TIME: 0.767 sec*)

________________________________
BTW, Shane, the above log output is only available in this format from SE.  It
makes a nice, readable report.
You asked some time ago why I wanted an improvement in the SD log panel.  This
is a good example.


I used an ASObjC timer handler written by Shane some time ago.

________________________________

use AppleScript version "2.4" -- Yosemite (10.10) or later

use framework "Foundation"

use scripting additions


--- DEMO of HANDLER ---



set theList to {5, 2, 6, 9, 4, current date, "TTT", 6, "TT", 3, weekday of
(current date)}

set longList to {}

repeat 4999 times

  set end of my longList to some item of theList

end repeat

set end of my longList to "T"


timer("Start")


set idxJM to my getIndex("t", longList)

timer("JM getIndex")

log ("idxJM: " & idxJM)


timer("STOP")


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

on getIndex(pItem, pList) -- @List @Index

  (*  VER: 2.0    2017-08-02

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

  PURPOSE:  Get the Index (Item #) of a Item within a List

  PARAMETERS:

    • pItem    ║ any  ║ Item to Find.  Can be text, numeric, date, or list

    • pList    ║ text  ║ List to be searched.

  RETURNS:  integer  ║ Index number (0 if NOT found)

  AUTHOR:  JMichaelTX

—————————————————————————————————————————————————————————————————————————————————

*)

  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

--~~~~~~~~~~~~~~~ END OF handler getIndex ~~~~~~~~~~~~~~~~~~~~~~~~~


________________________________


Best Regards,

Jim Underwood
aka JMichaelTX


From: Shane Stanley
<email@hidden<mailto:email@hidden>>
Date: Wednesday, August 2, 2017 at 11:10 PM
To: "ASUL (AppleScript)"
<email@hidden<mailto:email@hidden>>
Subject: Re: Position of an element in a list

Having suggested that approach, I'm now going to suggest it's not a great idea.
For small lists it's faster than the extra work in Nigel's script, but it
disappears as the list size increases. I don't think there's enough gain to
warrant the restriction of strings only, when Nigel's version doesn't have that
limitation.

I suspect a better approach is to check the number of items in the list. If
it's below a certain threshold, then use the plain AppleScript, otherwise use
Nigel's approach, something like this:

 _______________________________________________
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: Jim Underwood <email@hidden>
References: 
 >Re: Position of an element in a list (From: "Nigel Garvey" <email@hidden>)
 >Re: Position of an element in a list (From: Shane Stanley <email@hidden>)
 >Re: Position of an element in a list (From: Jim Underwood <email@hidden>)
 >Re: Position of an element in a list (From: Shane Stanley <email@hidden>)
 >Re: Position of an element in a list (From: Jim Underwood <email@hidden>)
 >Re: Position of an element in a list (From: Shane Stanley <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: Re: Position of an element in a list
  • Next by thread: Re: Position of an element in a list
  • Index(es):
    • Date
    • Thread