hash arrays and other mysteries
hash arrays and other mysteries
- Subject: hash arrays and other mysteries
- From: has <email@hidden>
- Date: Tue, 16 Oct 2001 17:58:28 +0100
Hi all,
Plunging straight into the deep end (this is what happens when you start
sniffing around other languages...), I'd very much like to use hash arrays
(that's the correct term, yes?) or their nearest sensible equivalent in
Applescript.
Only problem is that AS doesn't do them; the closest is records, but
there's no way in vanilla AS to define property names on the fly, which is
pretty essential if you're to retrieve values using a key. So:
get item "a" of {"a":1, "b", "hi", "c":false}
will have to remain a pipe dream, at least for now. Therefore, on to the
hacks...
Previously I was using stuff like:
======================================================================
getval({{"a", 1}, {"b", "hi"}, {"c", false}}, "a")
on getval(array, key)
repeat with eachitem in array
if eachitem's item 1 = key then return eachitem's item 2
end repeat
error
end getval
======================================================================
to look up a list for a sublist containing {key, value}. However, this sort
of approach offends my sense of neatness somewhat. Besides, I'd like
something a bit more "object-oriented" to play with.
So now I've got this, which uses Smile to cook the books a bit:
======================================================================
makehash({{"a", 1}, {"b", "hi"}, {"c", false}})
on makehash(array)
set str to ""
repeat with eachitem in array
set str to str & item 1 of eachitem & ":\"" & item 2 of
[NO-BREAK]eachitem & "\" as " & class of item 2 of eachitem & ", "
end repeat
tell application "Smile" to do script "{" & (characters 1 thru -3
[NO-BREAK]of str) & "}"
end makehash
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
The property names are supplied as a list of strings:
{{"a", [value]}, {"b", [value]}, {"c", [value]}}
These are assembled into a longer string:
"{a:\"1\" as integer, b:\"h\" as string, c:\"false\" as boolean}"
Smile's "Do script" command is then used to return this into a bona-fide
list object:
{a:1, b:"h", c:false}
Whoo! Well, not quite. This'll work for some value classes (string,
integer, real, boolean), but complex ones (e.g. lists and records) will
puke all over the place. I've not yet decided if this will be sufficient
(it might), or if I can figure a way to support adding lists and records
automagically as well.
And it's not vanilla either, but it's the closest I've managed to get. Has
anyone got any other ideas?
---
Also, anyone know any good resources (online/book) that cover the "design
theory"/"good practice" aspects of programming (as opposed to the "ignorant
amateurish hack" aspect which is what I'm currently familiar with?
Preferably something which can be translated into useable AS concepts
without too much of a leap, and written in terms that even a weakly-typed,
memory-managed, garbage-collected and otherwise mollycoddled sort such as I
can follow. (a.k.a. Perl and C wil hurt my poowr widdle head...)
---
Last but not least, does anyone know if it's possible to bounce between
using AS and the sparkly new JavaScript OSA within a script? e.g. By
writing a script server using AS, then calling its handlers from a
javascript. Or vice-versa, to allow an applescript to use a JS-based script
server. Lots of interesting possibilities there, assuming it can be done...
Cheers,
has