#! /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 # resources 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{$State} . "\.\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_abbreviations.html # and here: http://canadaonline.about.com/library/bl/blpabb.htm foreach my $pc (keys(%PostalCode)) { print "PostalCode for '$PostalCode{$pc}' is '$pc'\n"; } #### 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; #### .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 #### SVG drawing This was produced by version 4.1 of GNU libplot, a free library for exporting 2-D vector graphics. becomes 9 134720492145(17) necessary 10 164114526854434(2) human 6 131651875(3) in 2 3686(6) people 13 4647902198(22) course 4 4135697990(6) When 1 3344568(24) one 12 128504(24) events 7 4224378201(25) of 5 3882(10) the 3 133915(27) it 8 3692(12) for 11 118444(12)