x
Loading
 Loading
Hello, Guest | Login | Register

The Basics of Hashes

When I first started playing with awk more than two decades ago, I was amazed at the ease with which common tasks could be easily solved through the use of its “array” datatype. Prior to that, I had experienced only BASIC and C arrays, where the only index available was a small integer. But awk arrays could have arbitrary strings as keys!

When I first started playing with awk more than two decades ago, I was amazed at the ease with which common tasks could be easily solved through the use of its “array” datatype. Prior to that, I had experienced only BASIC and C arrays, where the only index available was a small integer. But awk arrays could have arbitrary strings as keys!

I remained mystified at how awk could manage to find the value for my key quickly. It took some study later to learn about hash tables and complex data structures, but luckily I didn’t need to know how it was done “under the hood” to be able to use them effectively.

As Larry Wall copied a lot of what was right about awk when designing and building the first version of Perl, one of the things he brought along was awk’s idea of an array with arbitrary string keys. He chose to call those associative arrays, to distinguish them from the more traditional small-integer-index arrays, which he also implemented in this new Perl language.

After a half-dozen years of Perl development, the term associative array was optimized to just hash for the Perl version 5 releases. I’m glad for that, because I use the term frequently when describing nearly every complex program I write.

A hash is a mapping of keys to values. We create elements in hashes by telling the hash that “this value goes with that key.” Later on, we can ask the…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace