Re: Records in handlers
Re: Records in handlers
- Subject: Re: Records in handlers
- From: has <email@hidden>
- Date: Mon, 6 Feb 2006 14:33:32 +0000
Jay Louvion wrote:
>set TheRecord to {Mine:{theAddress:"email@hidden", thelink:"http://www.ical.mac.com/jay/jay"}, His:{theAddress:"email@hidden", thelink:"http://www.ical.mac.com/jay/him"}}
>return theAddress of His of TheRecord
>
>But if the "His" is passed on as a param in a handler, or just from a text returned of dialog, it errors out.
This has nothing to do with handlers. The issue is that records simply aren't intended or designed for the purpose you want.
It's a common problem in AS: you've a bunch of values that you want to store under arbitrary keys. Lists aren't much good as-is as they only allow you to store values by indexed position. Records are useless because they're collections of properties accessed by reference, both of which have to be pre-defined in the program code.
For convenience, most languages include a suitable type as standard (e.g. Perl has its hash type, Python has its dictionary type) or as part of their standard library (e.g. the Foundation framework defines NSDictionary for use in ObjC). Python example:
d = {"foo":2, "bar":4}
print d["bar"] # --> 4
Unfortunately, AppleScript doesn't provide you with such a data structure as standard, so you have to build your own or use an existing third-party implementation, or use something that provides roughly equivalent functionality (e.g. ScriptDB or XMLLib osaxen). There are advantages and disadvantages either way, and neither's as convenient as having such a feature built-in, but you'll get by ok for most tasks.
For example, the following demonstration uses the AppleMods <http://applemods.sourceforge.net> Types library, which provides a ready-to-use 'Dictionary' object that's more than adequate for most tasks:
-- Boilerplate code to bind the Types library at compile-time:
property _Loader : (run application (get "LoaderServer"))'s makeLoader()
property _Types : _Loader's loadLib("Types")
-- Create a new Dictionary object:
set peopleInfo to _Types's makeDict()
-- Add values by key:
peopleInfo's setItem("foo", 2)
peopleInfo's setItem("bar", 4)
-- Get values by key:
peopleInfo's getItem("foo")
--> 2
OK, it doesn't have the nice concise syntax of the Python example (it's almost as verbose as using NSDictionary in ObjC), but it gets the job done.
Moral of the story is: when a language doesn't include the exact data structure you're after, you take whatever structures it does provide and build what you need out of those.
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden