Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Not Exactly a Hash Tutorial

by planetscape (Chancellor)
on Jul 01, 2005 at 08:38 UTC ( [id://471590]=perltutorial: print w/replies, xml ) Need Help??

last updated: 2008-12-11

N o t   E x a c t l y   a   H a s h   T u t o r i a l

Quick links:

Code Illustrating Hashes and Related Syntax


Hash Assignment
Hash Element Access
exists ( $hash{$key} )
keys( %hash )
delete ( $hash{key} )
values( %hash )
each( %hash )
Make an "Inverse" Hash
Let's Expand Our Real-World Example...
Visualizing a Hash Structure with FakeHash
A List of Hash Resources
Updates


Some Very Simple Code Illustrating Hashes and Related Syntax

#! /usr/local/bin/perl -w # myHash.pl # these are very simplistic syntax examples; # there's much more to know, so see the # resources below! use strict; use warnings; use Data::Dumper;
# Hash Assignment # In this case, a # 1 : 1 relationship # key => value my %StateName = ( AK => 'Alaska', AL => 'Alabama', AR => 'Arkansas', AZ => 'Arizona', CA => 'California', CO => 'Colorado', CT => 'Connecticut', DC => 'District Of Columbia', DE => 'Delaware', FL => 'Florida', GA => 'Georgia', HI => 'Hawaii', IA => 'Iowa', ID => 'Idaho', IL => 'Illinois', IN => 'Indiana', KS => 'Kansas', KY => 'Kentucky', LA => 'Louisiana', MA => 'Massachusetts', MD => 'Maryland', ME => 'Maine', MI => 'Michigan', MN => 'Minnesota', MO => 'Missouri', MS => 'Mississippi', MT => 'Montana', NC => 'North Carolina', ND => 'North Dakota', NE => 'Nebraska', NH => 'New Hampshire', NJ => 'New Jersey', NM => 'New Mexico', NV => 'Nevada', NY => 'New York', OH => 'Ohio', OK => 'Oklahoma', OR => 'Oregon', PA => 'Pennsylvania', RI => 'Rhode Island', SC => 'South Carolina', SD => 'South Dakota', TN => 'Tennessee', TX => 'Texas', UT => 'Utah', VA => 'Virginia', VT => 'Vermont', WA => 'Washington', WI => 'Wisconsin', WV => 'West Virginia', WY => 'Wyoming' ); my $href = \%StateName; # reference to the hash %StateName print Dumper $href; # note the order will differ from that + above print "\n";
# hash element access my $State = 'SD'; # planetscape's home state my $Name = $StateName{$State}; print "planetscape lives in " . $Name . "\.\n\n";
# exists ( $hash{$key} ) $State = 'QC'; # Quebec, Canada if (exists($StateName{$State})) { print "The abbreviation for $State is " . $StateName{$State} . "\. +\n\n"; } else { print $State . " is not a state! (yet)\n\n"; }
# keys( %hash ) my $count = keys %StateName; print "There are $count elements in the hash.\n\n"; foreach $State (keys(%StateName)) { print "State abbreviation is '$State'\n"; } print "\n";
# delete ( $hash{key} ) delete($StateName{DC}); # DC is not actually a state, # it's a postal abbreviation
# values( %hash ) $count = values %StateName; # $count is one less since we deleted DC print "There are $count elements in the hash.\n\n"; foreach $State (values(%StateName)) { print "State name is '$State'\n"; } print "\n";
# each( %hash ) while (my($key,$value)=each(%StateName)) { # there's lots more to +know print "key='$key', value='$value'\n"; # about "each" - see the # resour +ces below! } print "\n";
# make an inverse hash my %StateAbbreviation = reverse %StateName; # exists ( $hash{$key} ) $State = 'Quebec'; # QC if (exists($StateAbbreviation{$State})) { print "The abbreviation for $State is " . $StateAbbreviation{$Stat +e} . "\.\n\n"; } else { print $State . " is not a state! (yet)\n\n"; } print "\n";
# let's expand our real-world example from a hash of States # to a hash of postal abbreviations (not exhaustive) my %PostalCode = %StateName; # copy %StateName to %PostalCode $PostalCode{AS} = 'American Samoa'; $PostalCode{DC} = 'District of Columbia'; $PostalCode{FM} = 'Federated States of Micronesia'; $PostalCode{GU} = 'Guam'; $PostalCode{MH} = 'Marshall Islands'; $PostalCode{MP} = 'Northern Mariana Islands'; $PostalCode{PW} = 'Palau'; $PostalCode{PR} = 'Puerto Rico'; $PostalCode{VI} = 'Virgin Islands'; $PostalCode{PW} = 'Palau'; # many more postal codes and abbreviations for the US and Canada # may be found here: http://www.usps.com/ncsc/lookups/usps_abbreviatio +ns.html # and here: http://canadaonline.about.com/library/bl/blpabb.htm foreach my $pc (keys(%PostalCode)) { print "PostalCode for '$PostalCode{$pc}' is '$pc'\n"; }

Visualizing a Hash Structure with FakeHash

FakeHash is a module by Mark-Jason Dominus which was first announced here. I have elaborated slightly on the code presented in order to demonstrate some additional details such as setting drawing parameters. (More info on FakeHash can be found here.)

 

use strict; use warnings; use FakeHash; my %h = ( When => 1, in => 2, the => 3, course => 4, of => 5, human => 6, events => 7, it => 8, becomes => 9, necessary => 10, for => 11, one => 12, people => 13, ); open( my $FILEHANDLE, ">FakeHashDrawing.txt" ); my $fake = FakeHash::DrawHash->new; $fake->draw_param( 'BUCKET', [1.5, 0.75] ); $fake->draw_param( 'KVP' => [2.0, 0.75] ); while ( my ( $key, $value ) = each(%h) ) { $fake->store( $key, $value ); } $fake->draw($FILEHANDLE); close $FILEHANDLE;

This code produces the following output:

.PS boxwid:=1.5; boxht:=0.75 B00: box boxwid:=1.5; boxht:=0.75 B01: box with .n at B00.s circle at B01.c rad 0.1 filled arrow from B01.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N0100: box "becomes" "9" "134720492145(17)" boxwid:=1.5; boxht:=0.75 B02: box with .n at B01.s circle at B02.c rad 0.1 filled arrow from B02.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N0200: box "necessary" "10" "164114526854434(2)" boxwid:=1.5; boxht:=0.75 B03: box with .n at B02.s circle at B03.c rad 0.1 filled arrow from B03.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N0300: box "human" "6" "131651875(3)" boxwid:=1.5; boxht:=0.75 B04: box with .n at B03.s boxwid:=1.5; boxht:=0.75 B05: box with .n at B04.s boxwid:=1.5; boxht:=0.75 B06: box with .n at B05.s circle at B06.c rad 0.1 filled arrow from B06.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N0600: box "in" "2" "3686(6)" boxwid:=2; boxht:=0.75 N0601: box "people" "13" "4647902198(22)" boxwid:=2; boxht:=0.75 N0602: box "course" "4" "4135697990(6)" boxwid:=1.5; boxht:=0.75 B07: box with .n at B06.s boxwid:=1.5; boxht:=0.75 B08: box with .n at B07.s circle at B08.c rad 0.1 filled arrow from B08.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N0800: box "When" "1" "3344568(24)" boxwid:=2; boxht:=0.75 N0801: box "one" "12" "128504(24)" boxwid:=1.5; boxht:=0.75 B09: box with .n at B08.s circle at B09.c rad 0.1 filled arrow from B09.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N0900: box "events" "7" "4224378201(25)" boxwid:=1.5; boxht:=0.75 B10: box with .n at B09.s circle at B10.c rad 0.1 filled arrow from B10.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N1000: box "of" "5" "3882(10)" boxwid:=1.5; boxht:=0.75 B11: box with .n at B10.s circle at B11.c rad 0.1 filled arrow from B11.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N1100: box "the" "3" "133915(27)" boxwid:=1.5; boxht:=0.75 B12: box with .n at B11.s circle at B12.c rad 0.1 filled arrow from B12.c right boxwid/2 + 0.2 boxwid:=2; boxht:=0.75 N1200: box "it" "8" "3692(12)" boxwid:=2; boxht:=0.75 N1201: box "for" "11" "118444(12)" boxwid:=1.5; boxht:=0.75 B13: box with .n at B12.s boxwid:=1.5; boxht:=0.75 B14: box with .n at B13.s boxwid:=1.5; boxht:=0.75 B15: box with .n at B14.s .PE

Once you have generated FakeHashDrawing.txt with the above Perl code, you can convert it to PostScript with the following (under Cygwin/*nix):

pic FakeHashDrawing.txt | groff -c -Tps > FakeHashDrawing.ps

View the Output

(I then used Jasc Paint Shop Pro 8 to convert PostScript to PNG for display purposes.)

If you prefer a more "modern" standard such as SVG, you can use a utility such as plotutils' pic2plot to convert from Unix pic format to SVG or any other of a wide variety of formats. For example:

pic2plot -T svg FakeHashDrawing.txt > FakeHashDrawing.svg

Produces (on Cygwin):

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000303 Stylable//EN" "http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303-stylabl +e.dtd"> <svg width="8in" height="8in" viewBox="0 0 1 1" preserveAspectRatio="n +one"> <title>SVG drawing</title> <desc>This was produced by version 4.1 of GNU libplot, a free library +for exporting 2-D vector graphics.</desc> <rect x="0" y="0" width="1" height="1" style="stroke:none;fill:white;" +/> <g transform="translate(0.058854,-0.14453) scale(1,-1) scale(0.11458) +" xml:space="preserve" style="stroke:black;stroke-linecap:butt;stroke +-linejoin:miter;stroke-miterlimit:10.433;stroke-dasharray:none;stroke +-dashoffset:0;stroke-opacity:1;fill:none;fill-rule:even-odd;fill-opac +ity:1;font-style:normal;font-variant:normal;font-weight:normal;font-s +tretch:normal;font-size-adjust:none;letter-spacing:normal;word-spacin +g:normal;text-anchor:start;"> <rect x="0" y="-0.375" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <rect x="0" y="-1.125" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-0.75" r="0.1" style="stroke-width:0.010267;fill +:gray;"/> <line x1="0.75" y1="-0.75" x2="1.7" y2="-0.75" style="stroke-width:0.0 +10267;"/> <polygon points="1.6,-0.725 1.7,-0.75 1.6,-0.775 " style="stroke-width +:0.010267;fill:black;"/> <rect x="1.7" y="-1.125" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-0.56818) scale(1,-1) " style="font-fam +ily:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;basel +ine-identifier:centerline;stroke:none;fill:black;">becomes</text> <text transform="translate(2.7,-0.75) scale(1,-1) " style="font-family +:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline +-identifier:centerline;stroke:none;fill:black;">9</text> <text transform="translate(2.7,-0.93182) scale(1,-1) " style="font-fam +ily:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;basel +ine-identifier:centerline;stroke:none;fill:black;">134720492145(17)</ +text> <rect x="0" y="-1.875" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-1.5" r="0.1" style="stroke-width:0.010267;fill: +gray;"/> <line x1="0.75" y1="-1.5" x2="1.7" y2="-1.5" style="stroke-width:0.010 +267;"/> <polygon points="1.6,-1.475 1.7,-1.5 1.6,-1.525 " style="stroke-width: +0.010267;fill:black;"/> <rect x="1.7" y="-1.875" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-1.3182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">necessary</text> <text transform="translate(2.7,-1.5) scale(1,-1) " style="font-family: +'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline- +identifier:centerline;stroke:none;fill:black;">10</text> <text transform="translate(2.7,-1.6818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">164114526854434(2)< +/text> <rect x="0" y="-2.625" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-2.25" r="0.1" style="stroke-width:0.010267;fill +:gray;"/> <line x1="0.75" y1="-2.25" x2="1.7" y2="-2.25" style="stroke-width:0.0 +10267;"/> <polygon points="1.6,-2.225 1.7,-2.25 1.6,-2.275 " style="stroke-width +:0.010267;fill:black;"/> <rect x="1.7" y="-2.625" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-2.0682) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">human</text> <text transform="translate(2.7,-2.25) scale(1,-1) " style="font-family +:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline +-identifier:centerline;stroke:none;fill:black;">6</text> <text transform="translate(2.7,-2.4318) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">131651875(3)</text> <rect x="0" y="-3.375" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <rect x="0" y="-4.125" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <rect x="0" y="-4.875" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-4.5" r="0.1" style="stroke-width:0.010267;fill: +gray;"/> <line x1="0.75" y1="-4.5" x2="1.7" y2="-4.5" style="stroke-width:0.010 +267;"/> <polygon points="1.6,-4.475 1.7,-4.5 1.6,-4.525 " style="stroke-width: +0.010267;fill:black;"/> <rect x="1.7" y="-4.875" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-4.3182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">in</text> <text transform="translate(2.7,-4.5) scale(1,-1) " style="font-family: +'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline- +identifier:centerline;stroke:none;fill:black;">2</text> <text transform="translate(2.7,-4.6818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">3686(6)</text> <rect x="3.7" y="-4.875" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(4.7,-4.3182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">people</text> <text transform="translate(4.7,-4.5) scale(1,-1) " style="font-family: +'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline- +identifier:centerline;stroke:none;fill:black;">13</text> <text transform="translate(4.7,-4.6818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">4647902198(22)</tex +t> <rect x="5.7" y="-4.875" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(6.7,-4.3182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">course</text> <text transform="translate(6.7,-4.5) scale(1,-1) " style="font-family: +'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline- +identifier:centerline;stroke:none;fill:black;">4</text> <text transform="translate(6.7,-4.6818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">4135697990(6)</text +> <rect x="0" y="-5.625" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <rect x="0" y="-6.375" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-6" r="0.1" style="stroke-width:0.010267;fill:gr +ay;"/> <line x1="0.75" y1="-6" x2="1.7" y2="-6" style="stroke-width:0.010267; +"/> <polygon points="1.6,-5.975 1.7,-6 1.6,-6.025 " style="stroke-width:0. +010267;fill:black;"/> <rect x="1.7" y="-6.375" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-5.8182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">When</text> <text transform="translate(2.7,-6) scale(1,-1) " style="font-family:'H +elvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline-id +entifier:centerline;stroke:none;fill:black;">1</text> <text transform="translate(2.7,-6.1818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">3344568(24)</text> <rect x="3.7" y="-6.375" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(4.7,-5.8182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">one</text> <text transform="translate(4.7,-6) scale(1,-1) " style="font-family:'H +elvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline-id +entifier:centerline;stroke:none;fill:black;">12</text> <text transform="translate(4.7,-6.1818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">128504(24)</text> <rect x="0" y="-7.125" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-6.75" r="0.1" style="stroke-width:0.010267;fill +:gray;"/> <line x1="0.75" y1="-6.75" x2="1.7" y2="-6.75" style="stroke-width:0.0 +10267;"/> <polygon points="1.6,-6.725 1.7,-6.75 1.6,-6.775 " style="stroke-width +:0.010267;fill:black;"/> <rect x="1.7" y="-7.125" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-6.5682) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">events</text> <text transform="translate(2.7,-6.75) scale(1,-1) " style="font-family +:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline +-identifier:centerline;stroke:none;fill:black;">7</text> <text transform="translate(2.7,-6.9318) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">4224378201(25)</tex +t> <rect x="0" y="-7.875" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-7.5" r="0.1" style="stroke-width:0.010267;fill: +gray;"/> <line x1="0.75" y1="-7.5" x2="1.7" y2="-7.5" style="stroke-width:0.010 +267;"/> <polygon points="1.6,-7.475 1.7,-7.5 1.6,-7.525 " style="stroke-width: +0.010267;fill:black;"/> <rect x="1.7" y="-7.875" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-7.3182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">of</text> <text transform="translate(2.7,-7.5) scale(1,-1) " style="font-family: +'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline- +identifier:centerline;stroke:none;fill:black;">5</text> <text transform="translate(2.7,-7.6818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">3882(10)</text> <rect x="0" y="-8.625" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-8.25" r="0.1" style="stroke-width:0.010267;fill +:gray;"/> <line x1="0.75" y1="-8.25" x2="1.7" y2="-8.25" style="stroke-width:0.0 +10267;"/> <polygon points="1.6,-8.225 1.7,-8.25 1.6,-8.275 " style="stroke-width +:0.010267;fill:black;"/> <rect x="1.7" y="-8.625" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-8.0682) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">the</text> <text transform="translate(2.7,-8.25) scale(1,-1) " style="font-family +:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline +-identifier:centerline;stroke:none;fill:black;">3</text> <text transform="translate(2.7,-8.4318) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">133915(27)</text> <rect x="0" y="-9.375" width="1.5" height="0.75" style="stroke-width:0 +.010267;"/> <circle cx="0.75" cy="-9" r="0.1" style="stroke-width:0.010267;fill:gr +ay;"/> <line x1="0.75" y1="-9" x2="1.7" y2="-9" style="stroke-width:0.010267; +"/> <polygon points="1.6,-8.975 1.7,-9 1.6,-9.025 " style="stroke-width:0. +010267;fill:black;"/> <rect x="1.7" y="-9.375" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(2.7,-8.8182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">it</text> <text transform="translate(2.7,-9) scale(1,-1) " style="font-family:'H +elvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline-id +entifier:centerline;stroke:none;fill:black;">8</text> <text transform="translate(2.7,-9.1818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">3692(12)</text> <rect x="3.7" y="-9.375" width="2" height="0.75" style="stroke-width:0 +.010267;"/> <text transform="translate(4.7,-8.8182) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">for</text> <text transform="translate(4.7,-9) scale(1,-1) " style="font-family:'H +elvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseline-id +entifier:centerline;stroke:none;fill:black;">11</text> <text transform="translate(4.7,-9.1818) scale(1,-1) " style="font-fami +ly:'Helvetica',sans-serif;font-size:0.15152;text-anchor:middle;baseli +ne-identifier:centerline;stroke:none;fill:black;">118444(12)</text> <rect x="0" y="-10.125" width="1.5" height="0.75" style="stroke-width: +0.010267;"/> <rect x="0" y="-10.875" width="1.5" height="0.75" style="stroke-width: +0.010267;"/> <rect x="0" y="-11.625" width="1.5" height="0.75" style="stroke-width: +0.010267;"/> </g> </svg>

For more on visualizing complex data structures, please see Re: How can I visualize my complex data structure?


A List of Hash Resources

Online:
offsite:

Chapter 3 in Beginning Perl by Simon Cozens (PDF)

Chapter 5 of Picking Up Perl by Bradley M. Kuhn (PDF)

Section 2.3 in Impatient Perl by Greg London (PDF)

Steve's place - Perl Tutorial, by Steve Cook (archive)

Perl Training Australia's superlative materials:

What is Scalar Context? by Dominus

Understand References Today by Dominus

Hashes, as explained in PerlGuts Illustrated, by Gisle Aas

Perl.com: How Hashes Really Work, by Abhijit Menon-Sen

Effective Perl: Intermediate and Advanced Topics contains a wealth of information, not limited to hashes, but also including several pages on Joseph N. Hall's PErl Graphical Structures (PEGS).

Uri Guttman's tutorial on AUTOVIVIFICATION

PerlMonks Tutorials:

the basic datatypes, three, by root

Hash Keys (strings or numbers?), by robot_tourist

The Uniqueness of hashes, by injunjoel

Multidimensional Arrays, by CharlesClarkson

references, by busunsl

References quick reference, by tye

Object Serialization Basics, by chromatic

PerlMonks Categorized Questions and Answers:

Hashes

Other PerlMonks Nodes:

Extracting array of hashes from data by nysus

how to avoid mis-spelling hash keys?, by Gorilla

Perl Internals: Hashes, by Kozz

help with hashes, by Anonymous Monk

The Bad, the Ugly, and the Good of autovivification, by tlm

the third item on tye's scratchpad is a hash intro

Autovivification trick, by blazar

perldocs:

perlfaq4: Data Manipulation - Manipulating numbers, dates, strings, arrays, hashes, and miscellaneous data issues

perldata

perlref

perlreftut }

perldsc    }Thanks, wfsp!

perllol     }

Google:

Obligatory Google

 

Print:

Chapters 5 and 15 of Learning Perl by Randal L. Schwartz and Tom Phoenix

Pages 10-12 and 76-78 of Programming Perl, 3rd Ed., by Larry Wall, Tom Christiansen, Jon Orwant

Chapter 5 of the Perl Cookbook, 1st Ed., by Tom Christiansen, Nathan Torkington


Update: This is only a beginning. I noticed a lack of material on hashes in the Tutorials section, and thought I could contribute something, even though it might be one step at a time...

Update(s): Thanks to wfsp for pointing out... I forgot the perldocs!

2005-07-02: added perlfaq4, perlref, 125289, 137108, 69927, 224434

2005-07-06: several links per section; too lazy to enumerate

2005-07-07: added QandASection: hashes, tye's scratchpad, and some code

2005-08-07: added info on FakeHash, including code and graphics

2005-09-12: fixed several broken links, brought to my attention by Hue-Bond (Thanks!)

2005-12-18: added link to The Uniqueness of hashes, by injunjoel

2006-03-25: re-arranged code and references in more logical order; added anchors; added pic and SVG output and info on pic2plot

2006-03-26: added link to Object Serialization Basics, by chromatic

2006-08-13: added link to Autovivification trick, by blazar

2008-12-11: fixed several broken links, brought to my attention by pmonk4ever (Thanks!); also fixed a few b0rked tags

 

 

p l a n e t s c a p e

Replies are listed 'Best First'.
Re: Not Exactly a Hash Tutorial
by pmonk4ever (Friar) on Dec 11, 2008 at 21:27 UTC
    planetscape,

    Nice 'Not Exactly a Hash Tutorial' article...good job, hashes are certainly clearer now. It helps to cut and paste your examples and run them.

    The following links I noticed were broken:

    Hashes, as explained in PerlGuts Illustrated, by Gisle Aas

    Perl.com: How Hashes Really Work, by Abhijit Menon-Sen

    Effective Perl: Intermediate and Advanced Topics contains a wealth of information, not limited to hashes, but also including several pages on Joseph N. Hall's PErl Graphical Structures (PEGS)

    I received a 404 error for them...

    Other than that I appreciated all the examples you provided and the links that did work, I downloaded the documents...never know when they will disappear...

    Thanks again! :)

    pmonk4ever

    "No trees were harmed in the creation of this node. However, a rather large number of electrons were somewhat inconvenienced."

Re: Not Exactly a Hash Tutorial
by pmonk4ever (Friar) on Oct 08, 2009 at 00:28 UTC
    Thanks again, for updating the links on this node!

    Have a great day!

    It doesn't appear these Tutorials get much use, kinda dusty down here, *cough, cough* :>)

    pmonk4ever

    "No trees were harmed in the creation of this node. However, a rather large number of electrons were somewhat inconvenienced."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://471590]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2025-03-27 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (70 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.