Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there HashTable structure in Wolfram Mathematica?

I want to use a Structure like HashTable. Is there similar structure in Wolfram Mathematica?

like image 312
Yongwei Xing Avatar asked Sep 08 '09 04:09

Yongwei Xing


People also ask

What is hash in Mathematica?

A hash function projects a value from a set with many (or even an infinite number of) members to a value from a set with a fixed number of (fewer) members. Hash functions are not reversible. A hash function might, for instance, be defined as , where , , and.

What is the difference between Wolfram language and Mathematica?

Mathematica is a stand-alone product. Wolfram Desktop is actually a platform or interface to Wolfram Cloud products, such as Wolfram Programming Cloud. Mathematica can be licensed annually, or purchased outright. Mathematica requires an activation key.

What can you do with Wolfram Mathematica?

Wolfram Mathematica is a technical computing solution that provides businesses of all sizes with tools for image processing, data visualization and theoretic experiments. The notebook interface enables users to organize documents including texts, runnable codes, dynamic graphics and more.

Does Wolfram Alpha use Mathematica?

But now there's another interface to Wolfram|Alpha, one which brings with it a whole new set of capabilities: Mathematica. With the new Mathematica 8, you can access the Wolfram|Alpha engine directly from within Mathematica.


2 Answers

Update: Mathematica version 10 introduced the Association data structure (tutorial).


There are a number of possibilities. The easiest possibility, which works well if you don't need to add or delete keys from your table, or change their associated values, is to construct a list of rules with the key on the left-hand side and the value on the right-hand side, and use Dispatch on it.

If you do need to change the entries in your table, you can use the DownValues of a symbol as a hash table. This will support all the operations one commonly uses with hash tables. Here's the most straightforward way of doing that:

(* Set some values in your table.*) 
In[1]:=  table[a] = foo; table[b] = bar; table[c] = baz;

(* Test whether some keys are present. *)
In[2]:=  {ValueQ[table[a]], ValueQ[table[d]]}
Out[2]:= {True, False}

(* Get a list of all keys and values, as delayed rules. *)
In[3]:=  DownValues[table]
Out[3]:= {HoldPattern[table[a]] :> foo, HoldPattern[table[b]] :> bar,
HoldPattern[table[c]] :> baz}

(* Remove a key from your table. *)
In[4]:=  Unset[table[b]]; ValueQ[table[b]]
Out[4]:= False
like image 104
Pillsy Avatar answered Oct 16 '22 17:10

Pillsy


I'd say the most similar structure you can get out of the box are sparse arrays.

like image 43
João Silva Avatar answered Oct 16 '22 15:10

João Silva