Re: Getting elements of a list
Re: Getting elements of a list
- Subject: Re: Getting elements of a list
- From: KOENIG Yvan <email@hidden>
- Date: Sun, 20 Apr 2008 18:34:13 +0200
Le 20 avr. 2008 à 18:16, Matt Neuburg a écrit :
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.
--
Hello Mr. Neuburg
I apologize, but It seems that you forgot the original question:
is there any way,
without a repeat loop,
to pluck a specific item of each of the sublists?
Your responce is based on a loop !
Yvan KOENIG (from FRANCE dimanche 20 avril 2008 18:32:59)
_______________________________________________
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