Perl Hash Examples
I have a friend at work who occasionally writes in Perl and so has been learning the language over quite a while, picking up new bits when he needs to write something else. I'm generally his Perl sounding-board (among other topics) and he's getting pretty good at writing things from scratch himself now, even asking questions on things I've not done with Perl, not that I'm a massively advanced Perl programmer myself.
One of the things my friend regularly needs help with and I think confuses a lot of people with Perl is with variables, and in particular hashes. People often say to me they're not sure when to use dollar, percent or at-sign in their Perl variables. I knocked together some pretty noddy code to illustrate the various ways a hash can be used in Perl code as an example for him and thought it might be useful to a wider audience so posting it here.
The source code is below with the output from running this code presented afterwards. Syntax highlighting was done courtesy of perltidy.
#!/usr/bin/perl -w # # Some noddy hash examples # Note: the syntactical differences can be quite subtle # Note: hashes don't have guaranteed ordering (run this code) use strict; ################################################################################ # lets play with a hash ################################################################################ # define a variable as a hash with some content my %hash = ( 'a' => 1, 'b' => 2, 'c' => 3 ); print "print the hash value assigned to the key 'a'\n"; print $hash{"a"}."\n\n"; print "print all of the hash values\n"; foreach my $key (keys %hash) { print $hash{$key}."\n"; } print "\nprint all of the hash values in order\n"; foreach my $key (sort keys %hash) { print $hash{$key}."\n"; } print "print all of the hash values with associated keys\n"; foreach my $key (keys %hash) { print $key . " is " . $hash{$key}."\n"; } print "\nprint all of the hash values with associated keys in order\n"; foreach my $key (sort keys %hash) { print $key . " is " . $hash{$key}."\n"; } # get rid of the hash so we're 100% sure we're not using it below! undef %hash; ################################################################################ # lets play with a hash reference ################################################################################ # define a variable as a hash reference with some content my $hashref = { 'a' => 1, 'b' => 2, 'c' => 3 }; print "\n\nprint the hash reference value assigned to the key 'a'\n"; print $hashref->{"a"}."\n\n"; print "print all of the hash reference values\n"; foreach my $key (keys %{$hashref}) { print $hashref->{$key}."\n"; } print "\nprint all of the hash reference values in order\n"; foreach my $key (sort keys %{$hashref}) { print $hashref->{$key}."\n"; } print "print all of the hash reference values with associated keys\n"; foreach my $key (keys %{$hashref}) { print $key . " is " . $hashref->{$key}."\n"; } print "\nprint all of the hash reference values with associated keys in order\n"; foreach my $key (sort keys %{$hashref}) { print $key . " is " . $hashref->{$key}."\n"; }
print the hash value assigned to the key 'a' 1 print all of the hash values 3 1 2 print all of the hash values in order 1 2 3 print all of the hash values with associated keys c is 3 a is 1 b is 2 print all of the hash values with associated keys in order a is 1 b is 2 c is 3 print the hash reference value assigned to the key 'a' 1 print all of the hash reference values 3 1 2 print all of the hash reference values in order 1 2 3 print all of the hash reference values with associated keys c is 3 a is 1 b is 2 print all of the hash reference values with associated keys in order a is 1 b is 2 c is 3