[comp.sys.mac.hypercard] Association lists in hypercard?

gasp@bu-cs.BU.EDU (Isaac Kohane) (08/30/89)

Hi,

	I'm a relative novice to hypercard and have some trouble
finding a clean solution to the creation of association lists. That is,
I'd like to store pairs of keys and their values and wish to be able
to retrieve the value given the key. Any ideas?


				-zak

gasp@bu-cs.BU.EDU (Isaac Kohane) (08/31/89)

P

Paul Biron sent me the following useful reply.

				-Zak

sirkm@ssyx.ucsc.edu (Greg Anderson) (08/31/89)

In article <37303@bu-cs.BU.EDU> gasp@bucsd.bu.edu (Isaac Kohane) writes:
>Hi,
>
>	I'm a relative novice to hypercard and have some trouble
>finding a clean solution to the creation of association lists. That is,
>I'd like to store pairs of keys and their values and wish to be able
>to retrieve the value given the key. Any ideas?
>
>
>				-zak

You could use the 'grep' XFCN to do this.  Store keys and values in
a field like this:

	key1,value1
	key2,value2
	key3,value3

... and so on.  Then:

	get grep("key2",bg field "whateveritsnameis")

will return

	key2,value2

Use 'item 2 of it' to reference the value, or just say 'get item 2 of grep('.

To delete a key and its value:

	put grep(v,"key2",bg fld "xx") into bg fld "xx"

To add a key, just put it after the field.  Don't forget the return --
grep works on lines.

To change the value of a key, just delete and re-add it.

An alpha version of the grep XFCN is attached to the HyperUnix home
stack, available for anonymous ftp from ssyx.  There's still a bug
in wildcard searching, though.  As soon as I fix that, I'll put grep
and replace (sed?) up for anonymous ftp.

  ___\    /___               Greg Anderson              ___\    /___ 
  \   \  /   /         Social Sciences Computing        \   \  /   /
   \  /\/\  /    University of California, Santa Cruz    \  /\/\  /
    \/    \/              sirkm@ssyx.ucsc.edu             \/    \/