The hash Type
A hash is a type which works like a dictionary: it allows expressions ("values") to be stored and looked-up using other expressions as the "keys". The following example shows a steve hash being used like a dictionary:
dictionary (hash).
dictionary{ "dog" } = "a four-legged pet".
dictionary{ "fish" } = "a zero-legged pet".
# this will print the definition we stored above.
print "the definition of dog is: ", dictionary{ "dog" }.
As shown in this example, we're able to store data using strings as the keys. When data is later retrieved from the hash table using the same key, the value stored previously is returned.
hashes are not limited to using strings as keys. They can use any valid type. The most useful application is a hash table which uses objects as keys. In this way, relationships between objects can be stored in a hash. Consider an object that wants to keep track of neighbors it has encountered previously:
# let's say that this method gets called when a neighbor is seen...
# if the neighbor has already been seen, print a message — otherwise,
# just add it to the hash for next time!
+ to meet with neighbor (object):
if seenHash{ neighbor }: print "I've seen this neighbor before!"
else seenHash{ neighbor } = 1.
The example above also shows that the syntax of hashes is the same as the syntax for lists when storing or retrieving data. Unlike lists, however, hashes do not hold ordered data, and none of the other list operators work with hashes.
When using hashes with keys that are ints, floats, vectors, matrices or strings, steve will test the equivalence of the keyed data when looking up or storing the data. This means that two equivalent strings, even if they are stored in different variables, will refer to the same value in the hash. For the other types (objects, pointers, data, lists and hashes themselves), the hash will only return the same value for the same exact variable key. This means that two different lists, even if they contain "equal" data, will access different values in the hash.
An important feature of hashes in steve is that they are always passed by reference and are not copied. This means that if you pass a hash to a function, then any modifications done to the hash inside the function will modify the original hash.
