Hash Table structure in mathematica 7

Hello,

can anybody tell me how tp program a hash table data structure in mathematica 7?

I search in the Internet a little but, did not find much or did not understand the code. What I found so far:

Creation of a hash table:

TableName[key]=value

List all the keys?
List all the values?
Delete key ?
Delete value?

Thank you for your help

Check the documentation for DownValues, Set, and Unset. For things that are free of explicit patterns, something like

myTable[key1] = val1;
myTable[key2] = val2;

is effectively a hash table with key and key2 as keys, etc. (and they really are hashed, under the hood). To remove one of these use Unset; I show the infix version below.

myTable[key1] = val1;                                                   
myTable[key2] = val2;               

We look up key:

In[9]:= myTable[key1]                                                           

Out[9]= val1

Now clear it and try that lookup. It will fail to find a value.

In[10]:=  myTable[key1] =.                                                      

In[11]:= myTable[key1]                                                          

Out[11]= myTable[key1]

Hello Daniel,

thank you for your reply.

Did I understand you right to get a list of all keys or values I have to use Downvalue?
I had a look in the documentation. In fact the output contains the keys and values, but also other stuff. So I need to filter/extract the keys/value, am I right?
Or is there a way to get a “pure” list of keys and/or values?

thx again
Murad

You are correct on all counts.

Version 10 has Association which provides a means to get Keys and Values separately, amongst numerous other features. It too has a hash table under the hood. One caveat is it might not be as useful if one plans to build it up or modify in many steps.

ok got it.

thank you Daniel…