Re: associative arrays

• To: mathgroup at smc.vnet.net
• Subject: [mg58180] Re: [mg58137] associative arrays
• From: Sseziwa Mukasa <mukasa at jeol.com>
• Date: Tue, 21 Jun 2005 06:02:57 -0400 (EDT)
• References: <d8rnfg\$l4b\$1@smc.vnet.net> <200506181007.GAA08771@smc.vnet.net> <200506190744.DAA05732@smc.vnet.net> <E89993C2-F308-430B-9064-8248DEF9D0C8@jeol.com> <20050620193157.GA7201@mdssdev05.comp.pge.com>
• Sender: owner-wri-mathgroup at wolfram.com

```On Jun 20, 2005, at 3:31 PM, Ed Peschko wrote:

> On Mon, Jun 20, 2005 at 09:05:21AM -0400, Sseziwa Mukasa wrote:
>
>>
>> On Jun 19, 2005, at 3:44 AM, Ed Peschko wrote:
>>
>>
>>> hey all,
>>>
>>> Is there a simple way to make an associative array in mathematica?
>>>
>>> I noticed that there's a sparse array, but I'd like to be able to
>>> use a non-numeric value as key..
>>>
>>
>> Don't use arrays, use pattern matching eg:
>>
>> f[key1]=value1
>> f[key2]=value2
>> .
>>
>
> that does some things that I want, but I'm not sure if this is a
> 'true'
> associative array, like perl's or python's dictionaries..

Using expressions in this manner is perhaps not as elegant as Perl or
Python but I think it meets your needs.

> Can you:
>
>     a) list the keys in a given order,

In[123]:=
f[key1] = value1
f[key2] = value2
DownValues[f][[All, 2]]
Out[123]=
value1
Out[124]=
value2
Out[125]=
{value1, value2}

Use Sort on the last expression to modify the order.

> and iterate over them?

I'm not sure what you mean by this, this expression applies g to the
values in f.

g/@DownValues[f][[All,2]]

>     b) do true, multi-dimentional hashes, where both the key and
>            the value of the hash are either other hashes, or lists?

There is no restriction on the value of either the keys or values,
other than that they are valid Mathematica expressions.  You probably
don't want to use patterns for values though.

> I know you can do ?f to list the values, but I'd like to be able to do
> the 2 items above programatically.
>

Hope this helps,

Ssezi

```

• Prev by Date: Re: field line with NDSolve - attachment on www
• Next by Date: Re: Re: can anyone solve this equation?
• Previous by thread: Re: Re: associative arrays
• Next by thread: Re: Transformation rule problem