Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

understanding Hashes 1

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
US
Hello! I am new to Perl and have been using it intermittently in a QA role. Mainly for parsing & testing tab delimited files.

I am reading through the Learning Perl book & working through Chapter 5 right now which is a chapter on hashes.

I am not sure I'm understanding a Hash on the large scale. I understand everything that is presented in the book - but I often come across reference to an MD5 hash. Not having a full understanding of what an MD5 hash is ... I'm trying to discern if a Perl hash and an MD5 hash are essentially of the same ilk - or if these are very different concepts.

Could someone enlighten me?

Thank you!
 
There's no relation between the two.

An MD5 hash is basically a checksum of a file; after you download a file, you can run a program called md5sum to calculate its MD5 hash and check it against the published hash. If they are different, the downloaded file has been corrupted.

A Perl hash is a data structure of key/value pairs. The expression $foo{'bar'} refers to the value corresponding to the key 'bar' in the hash called 'foo'.
 
a perl hash and an MD5 hash are two entirely different things.
 
thank you for the info! another question - do you find that you use Hashes often in your perl programming? I am understanding the concept as it's presented in the book - but not sure how i will apply it in-real-life.
 
Actually, there is a relationship.

MD5 is a hash *function*, i.e. it takes some data and creates a fingerprint from it.

Term "hash" when used in a Perl data structure context is short for hash *table*. A hash table uses some hashing function to locate the values for a given key. The hashing function is applied to the key and the resulting fingerprint indicates where the corresponding value is/should be stored. MD5 would be too heavyweight for use in hash tables, though it would be possible. Because it's designed with cryptography in mind, it has to be extremely robust and is therefore too slow to be used for efficient programming.

The wikipedia articles on "Hashing Function" and "Hash Table" contain a pretty good explanation, though they may be heavy going in places.

As for using hashes in Perl programs, there are two distinct steps that I went through:
1) Understanding how they work
2) Understanding how incredibly bloody useful they are

The first stage is the easy one initially! It's worth keeping an eye on these kinds of forums for uses of hashes. There are some questions for which you'll think you have a good answer written using arrays, but that's until you see the one-liner that someone else comes up with using a hash. IMO there's nothing like learning from observation when it comes to figuring out when to use the tools that are available to you.
 
Very simple example of two implementations of a simple dictionary. The hash-based one is far more efficient, as it won't have to loop through a load of definitions (obviously there'd be more in real life situations :)):
With arrays:
Code:
# set up data structures
my @terms = qw/Perl hashtable ishnid/;
my @definitions = (
   'A really useful programming language',
   'Some kind of data structure using a hash function',
   'Some idiot from the forums'
);

# I want to get a definition for 'Perl'
my $to_find = 'Perl';
for ( 0 .. $#terms ) {
   if ( $terms[ $_ ] eq $to_find ) {
      print "$to_find: $definitions[ $_ ]\n";
      last;
   }
}

With a hash:
Code:
my %definitions = (
   Perl => 'A really useful programming language',
   hashtable => 'Some kind of data structure using a hash function',
   ishnid => 'Some idiot from the forums'
);

my $to_find = 'Perl';
print "$to_find: $definitions{$to_find}\n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top