• 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: Getting record items of a sublist
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting record items of a sublist


  • Subject: Re: Getting record items of a sublist
  • From: Christopher Stone <email@hidden>
  • Date: Sun, 30 Oct 2011 11:50:00 -0500

On Oct 30, 2011, at 06:41, Nigel Garvey wrote:
This takes about 0.02 seconds:

 repeat with i from 1 to (count theNameList)
   set theItem to myname of item i of my theNameList -- NB. 'theNameList' is referenced (with 'my').
   set end of my theChooseList to theItem -- Ditto 'theChooseList'.
 end repeat
______________________________________________________________________

Hey Nigel,

My, my.  That's a whopper of a difference.

0.008 seconds here on my i7 MacBook Pro

Outstanding little trick - any idea of why it works?

For contrast:

Using Axel's original data sample of 2016 records, changing it to pure text and then doing the lookup with the Satimage.osax's 'find text' command with regular expressions.

set lookupTable to "
myname:Titzi        height:170          weight:55
myname:Stephen      height:184.5        weight:75
myname:Elke         height:174.5        weight:50
...
"

fndUsing("myname:([^\\t]+)", "\\1", lookupTable, false, true, false, true, true)  ## My Handler

 0.0141 seconds

Tabs are changed to spaces for readability in Mail.

Shelling out to Perl instead:

set cmd to "perl -wlne 'if (m/^myname:([^\\t]+)/) {print \"$1\"}' <<< " & quoted form of lookupTable
set AppleScript's text item delimiters to return
set chooseFrom to text items of (do shell script cmd)

 0.0348 seconds

And just for giggles let's try out plain old sed:

set f to alias "Thor:Users:chris:test_directory:junksio.txt"
set lookupTable to quoted form of (read f)
set cmd to "sed -nE 's/^myname:([^ ]+).+/\\1/p' <<< " & lookupTable
set AppleScript's text item delimiters to return
set chooseList to text items of (do shell script cmd)

 0.0674 seconds

I've done these kinds of lookups since 1996 or so when I discovered the first regex osax, and I prefer that method for speed, flexibility, and ease of maintenance.

I often keep longer lists as separate text files for convenient editing/sorting with BBEdit and read them into the script at runtime.

Using the original Satimage.osax script but reading input from a file:

 0.0147 seconds

Given this kind of fixed record structure there's no need for field labels, although I will often put in a label header for readability:

set lookupTable to "
FNAME       LNAME           HEIGHT          WEIGHT
--------------------------------------------------          
Travis      Mcgee           193             95
"

Again tabs are changed to spaces for readability in Mail.

One more thing.  Let me recommend the LapTime.osax for timing scripts - or the 'chrono' function in Smile.  (These are free.)

And of course Script Debugger has its own timing mechanism (but it's not free).

--
Chris

 _______________________________________________
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: Getting record items of a sublist
      • From: Alex Zavatone <email@hidden>
References: 
 >Re: Getting record items of a sublist (From: Nigel Garvey <email@hidden>)

  • Prev by Date: Re: Getting record items of a sublist
  • Next by Date: Re: Getting record items of a sublist
  • Previous by thread: Re: Getting record items of a sublist
  • Next by thread: Re: Getting record items of a sublist
  • Index(es):
    • Date
    • Thread