Getting a record item with a string key -- a "real programmer" kind of hack
Getting a record item with a string key -- a "real programmer" kind of hack
- Subject: Getting a record item with a string key -- a "real programmer" kind of hack
- From: Olof Hellman <email@hidden>
- Date: Tue, 09 Oct 2001 14:45:05 -0700
It has been suggested that the usrf method is useless without the reverse
operation, i.e. not just making the list, but getting items out of the list.
List Guy posted Neal Crocker's object-specifier based version. I think this
one is easier to grok, although it uses one of the worst features of
AppleScript ( i.e. handler assignment inside a script object ):
to usrf(theList)
script
{+class usrf;:theList}
end script
run script the result
end usrf
to extract_usrf(theRecord, fieldName)
script Fred
property r : theRecord
to extract() -- supply a dummy method
end extract
end script
set Amy to run script "script George
property r : {}
to extract()
return " & fieldName & " of r
end extract
end
George"
set Fred's extract to Amy's extract
return (Fred's extract())
end extract_usrf
set usrfList to usrf({"a", 1, "b", 2})
-->{a:1, b:2}
-- now use the string "b" to get b
extract_usrf(usrfList, "b")
--> 2