Re: Getting elements of a list
Re: Getting elements of a list
- Subject: Re: Getting elements of a list
- From: Matt Neuburg <email@hidden>
- Date: Sun, 20 Apr 2008 09:16:16 -0700
- Thread-topic: Getting elements of a list
The original question was:
> set myList to {{1,2},{3,4},{5,6}}
>
> is there any way, without a repeat loop, to pluck a specific item of
> each of the sublists? something like
>
> first item of each item of myList
>
> doesn't work...
It seems to me that what the OP is *really* asking for is not a loopless way
(which recursion in AS cannot truly supply, as sooner or later, with a long
enough list, you'll get a stack overflow because AS does not implement
tail-recursion properly) but a *general* way. This is a topic that my book
covers. Here's an example:
on map(L, h)
script s
property hh : h
set myL to {}
repeat with i in L
set end of myL to hh(i)
end repeat
myL
end script
run s
end map
"map" is saying: You hand me a list *and a function* (handler), and I'll
apply the function to every member of the list and hand back the resulting
list. Every list-handling language needs something like this, so why not
AppleScript? So:
set L to {{1, 2}, {3, 4}, {5, 6}}
on h1(what)
item 1 of what
end h1
on h2(what)
item -1 of what
end h2
map(L, h1) -- {1,3,5}
map(L, h2) -- {2,4,6}
m.
PS.:
On Sat, 19 Apr 2008 16:38:59 +0200, Skeeve <email@hidden> said:
>Offtopic: I prefer:
>
>a^=b^=a^=b
>
>to exchange two variable's content...
Still offtopic: I prefer
a,b = b,a # guess what language this is?
But, you know something? It works in AppleScript too:
set a to 1
set b to 2
set {a, b} to {b, a}
display dialog a
display dialog b
There was never any need for a *real* list in that story at all... It's
effectively just a notation.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
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