Megabite #22 - Hashtables

In the last Megabite, I covered simple arrays - again, arrays are a huge deal and make coding and variable storing a much easier and more dynamic process.  One thing that an array lacks though is a searchability.  From an array, you can get the amount of entries or the values of individual entries, but you'd have to write a function to go through each thing one-by-one if you were looking for a specific entry or object.

This is where hashtables come in, and they are also wonderful.  Like an array, a hashtable holds both data and a key, but it does it in a little bit of a different format.  On the C# side, a hashtable is called a dictionary, and that's a good way of looking at it - its an indexed, searchable variable holder.

Creating your first hashtable is easy:

var myHash : Hashtable;
myHash = new Hashtable();

This function is code-only as far as I know - it's not something you can simply add via the inspector.  To start it off, we're going to add a few strings into it with the Add function.  Something like this:

function Start() {
var myHash : Hashtable;
myHash = new Hashtable();
myHash.Add(1, "red");
myHash.Add(2, "blue");
myHash.Add(3, "green");
myHash.Add(4, "yellow");
}

What we have here will basically create these 4 entries into our dictionary when the script begins.  Like an array, we can access things by key like so:

print (myHash[1]);

This will output "red", as expected.  You'll notice that we don't start at zero like an array would.  The "pages" of a hashtable work differently.  We actually don't have to go in any kind of order (though you will most likely choose to anyway).

if you do go in order, you can use most functions just as you would with an array without much hassle.  If you're overwriting an existing key that you've already add, you simply want to use the Remove() function on that key before adding a new entry.  Add() will not actually overwrite without an error.

The big advantage however is that a hashtable allows the use of ContainsValue() and ContansKey().  These functions will output a boolean true/false that works great with IF statements.  There are a multitude of reasons to use this type of functionality, but as you Add and Remove entries throughout your code, you can use these as triggers or safety nets.

if (myHash.ContainsValue("purple")) {
print ("Purple was found in the dictionary.");
DoSomeOtherThing();
}
if (!myHash.ContainsKey(6)) {
print("Page 6 is missing from the dictionary.");
}

Other functions for hashtables include Clear() and Count - doing something like: myHash.Clear(); will simply delete all entries within the hash, so it's not something to use lightly.  Count will return the total number of "pages" in your hashtable in the same manner that myArray.Length would do.  Count is not a function though, so it would be used something like this:

print("myHash contains a total of " + myHash.Count + " entries.");

While this isn't something that I expect everyone will have immediate need of, it's one of those great things that come in handy when you want to want to use an array and have finer control or general search indexing available.  For myself, I know that this little addition has been a lifesaver in certain scenarios, and I hope you get the chance to use it for yourself.

As always, let me know if you have any questions in the comments below!