Re: Coercion of list to record
Re: Coercion of list to record
- Subject: Re: Coercion of list to record
- From: Christopher Nebel <email@hidden>
- Date: Wed, 6 Feb 2008 14:15:52 -0800
On Feb 6, 2008, at 12:19 PM, Robert R. Horning wrote:
I wish to create a list of records at run-time. Both the name
(property) and value of each record in the list will come from an
Excel workbook. The names will equal the Excel column headers and
the values will be the ranges of the columns that include those
headers. I can extract the necessary data from the workbook, and
can create a list consisting of:
{(columnHeader1: range1), (columnHeader2: range2), ...},
where columnHeaders and ranges are variables containing the
retrieved data. But so far I've been unable to coerce the list of
variables containing strings into a list of records.
As a start, I've tried to use a technique found in Neuburg, M.
(2006) Applescript, the Definitive Guide, p253. Here's his code:
on listToRecord(L)
script myScript
return {<<class usrf>> :L} --(The << and >> are really guillemot
characters.)
end script
end listToRecord
set R to listToRecord({"name", "haha", "age", 51})
log |name| of R --Expect to get (*haha*), get (*name*). (Note
'pipe' symbols enclosing name in log statement.)
log age of R --Expect to get (*51*), get (*age*)
log nonsense of R --Expect to get error message, get (*nonsense*)
It may have worked better than you think; "log" is a little perverse
that way. Try saying "log (get age of R)". That said, "get age of R"
doesn't work for me, which means I have to track down when it did work
and why it failed. I hate it when people come up with clever tricks
exploiting the implementation.
May I point out, however, that since the record labels are coming from
Excel, how do you know what they are so you can refer to them in your
script? Either you just know because they're static, in which case
there are other ways to create the record using normal keys, or you
don't know because they're dynamic, in which case you need to do the
same string-to-key translation whenever you want a value, which is
going to suck.
Records are *not* associative arrays. An associative array, if you
didn't already know about them, simply associates values (keys) with
other values, and none of the values are determined until run time.
(They're sometimes called "hashes", "maps", or "dictionaries".) A lot
of people look at AppleScript records and think that they're
associative arrays -- the keys look like text, after all -- but
they're really more analogous to a C struct or a Pascal record:
they're a set of labeled fields *where the labels are all known at
compile time*. There are ways to bludgeon AppleScript records into
serving as associative arrays, but it's just kind of painful. There
are a few different modules that folks have written to do real
associative arrays; go use one of them.
--Chris Nebel
AppleScript Engineering
_______________________________________________
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