Re: why does DownValues not return all downvalues for a symbol?

*To*: mathgroup at smc.vnet.net*Subject*: [mg99425] Re: why does DownValues not return all downvalues for a symbol?*From*: Szabolcs <szhorvat at gmail.com>*Date*: Tue, 5 May 2009 06:01:19 -0400 (EDT)*References*: <gtnk2p$3l6$1@smc.vnet.net>

On May 4, 11:44 pm, Michael <michael2... at gmail.com> wrote: > I'm trying to get the functionality of "associative arrays" or "alist" > or "hash tables" to work in Mathematica. However I'm seeing a quirk > and I wonder if anybody can explain what is going on or suggest a > better approach? > > I can define a simple associate array like so: > f["red"]=1; > f["blue"]=2; > ... > If all my parameters are strings, then I can find the "keys" to my > alist with the following function: > StringKeys[y_] := DownValues[y] /. {( _[y[str_String]] :> _) -> str}; > SetAttributes[StringKeys, HoldFirst]; > > In[141]:= StringKeys[f] > > Out[141]= {"blue", "red"} > > However, if I try to define a more complex alist, I cannot find any > way to retrieve the definition! > > In[142]:= g["x"]["y"] = 1; > That's a SubValue, not a DownValue. But why not use g[{"x","y"}] or something similar? You should also consider using a list of Rules (possibly with Dispatch).