Getting a record item with a string key -- improved
Getting a record item with a string key -- improved
- Subject: Getting a record item with a string key -- improved
- From: Olof Hellman <email@hidden>
- Date: Wed, 10 Oct 2001 12:29:48 -0700
Here's a variant that works around the issues of canonicalization of
variable names, so that you can use uppercase and multi word tags in your
dynamically built-from-string records, as was originally requested by
Harald:
to extract_exact_usrf(theRecord, fieldName)
script Fred
property r : theRecord
to extract()
end extract
end script
set Amy to run script "script George
property r : {}
to extract()
return |" & fieldName & "| of r -- note the bars!!
end extract
end
George"
set Fred's extract to Amy's extract
return (Fred's extract())
end extract_exact_usrf
to usrf(theList)
script
{+class usrf;:theList}
end script
run script the result
end usrf
set usrfRecord to usrf({"EU", 3, "b", 2})
-->{|EU|:3, b:2}
-- now use the string "EU" to get record item |EU|
extract_exact_usrf(usrfRecord, "EU")
--> 3
Note that you'll still need the previous version if you want to do
set aRecord to {EU: 3, b: 2}
-->{EU:3, b:2}
-- not built with usrf() , so the record tag is EU, not |EU|
-- now use the string "EU" to get record item EU
extract_usrf(usrfRecord, "EU")
--> 3
- Olof