Re: Array ??
Re: Array ??
- Subject: Re: Array ??
- From: has <email@hidden>
- Date: Tue, 8 Jan 2002 12:34:09 +0000
Phi Sanders wrote:
>
in applescript the list object takes the place of arrays
>
ex:
>
set vList to {"a","b","c","d"}
>
set vItem to item 3 of vList
>
--sets vItem to "c"
Hi rhyme, if Phil's already answered your question then great; if not, read
on...
In addition to lists, AS also has things called records which can store
values by label rather than position (aka index). For example:
set theRec to {bob:1, mary:4}
get bob of theRec
--> 1
The bad news is that the labels are identifiers, which have to be defined
in your original code. As with variable names, there's no way for these to
be generated on-the-fly.
This means you can't create new label-value pairs in records while your
script is running. To do this you need hash arrays. These can take strings
as labels, and would probably look something like:
{"bob":1, "mary":4}
if Applescript had had them. Which unfortunately it doesn't (most other
languages do). Chris Nebel has stated previously on this list that they're
on the TO-DO list, though when it'll happen is anyone's guess.
Meantime, if neither lists or records are sufficient for you, you have
several options to get around this limitation:
- go use a different language (Wait, I was kidding!)
- use one of the various 3rd-party libraries that have been written by
other users to address this gap. You'll find a couple in the AppleMods
section of macscripter.net, e.g. AssociativeLib, TableSearchServices, and
you can likely blag other variations off folk in this list (e.g. me:)
- use a scripting addition called RecordAccess, which *does* allow you to
manipulate records as if they were hashes (available on osaxen.com; I don't
know if it's OS X-savvy)
HTH
has
p.s. Once you get familiar with using lists and records, you might want to
be aware of p206 of the Applescript Language Guide. It discusses one of the
less obvious "gotchas", a hidden technique called "Data Sharing" that AS
uses to improve its efficiency, that can sometimes catch folk out when they
start doing more complex stuff with lists/records.