Scripting Languages Chapter 5 Hashes
Hash Data structure, not unlike an array – it will hold any number of values It indexes values by name – not by an indices Hash indices are called keys – not numbers but arbitrary strings
Keys Are strings – instead of accessing element number 3 we’ll access the hash element named Monday You can use any string expression for a hash key – just as there’s only one array element 3 – there’s only one hash element - Monday
Associative Array Hash = = Associative Array Key - > Value Key is used to receive the value This data type is used to implement table lookups. Key is usually string data – thus lookup capital city by its state or lookup bank balance by the name of the account
Associative Array’s Cont’d They save the programmer the actual programming of the lookup Define a hash with % then pairs must be specified. %accounts = (Mike => 100, Sue => 200, Erin => 150, Patti => 250, );
Associative Array’s Cont’d Is the symbol used to make the associations clear First two entries represent first pair …… %accounts = (Mike,100,Sue,200,Erin,150,Patti,250); An associative array and a regular array may have the same name as they exist in their own namespaces.
Hashes Cont’d You must use { } to extract a particular value $accounts{Mike} #yields 100 $accounts{Sue} # yields 200 The key that acts as the subscript may be quoted or not – if the key contains embedded blanks, then the key must be quoted
#!/usr/bin/perl –w # hash.pl %accounts = (Mike => 100, Sue => 200, Erin => 150, Patti => 250 ); print “Enter a name “; $name = ; #read a name chomp ($name);#remove newline character print “$name has balance: $accounts{$name}\n”;
Hash Functions besides using a key to retrieve a value, a common activity on hashes is to get a list of all the keys or a list of all the values keys & = = values(%accounts); delete function – delete $accounts{Mike}; grow a hash simply by adding to it: $accounts{Maria} = 100; $accounts{Dave} = 100;
More Hash Functions each functions – allows you to iterate over an entire hash – returns a key – value pair as a two element list. while ( ($key, $value) = each %hash) { print “$key => $value \n”; }
sort function sorts your has so that key value displays in a sorted fashion foreach $key ( sort keys %hash) { $value = $hash($key); print “$key => $value \n”;
exists Function to see whether a key exists – returns a true value if it exists if (exists $books {“Tolkien”}) { print “Book found \n”; }
#!/usr/bin/perl -w # This program assigns an array to a hash # and displays the keys and the values # of the hash # create an = qw( one 1 two 2 three 3 four 4 five 5 ); print "\nThe array # A loop to assign array to a hash for( $key=0; $key<10; $key +=2 ) { $hash{$array[$key]}=$array[$key + 1]; } # A loop to print the hash while (($key, $value) = each(%hash)) { print "$key => $value \n" }
#!/usr/bin/perl -w sub create { my($name, $age, $city) my $emp = { name => $name, age => $age, city => $city }; return $emp; } while ( ) = split; $record = $record); } { $x = print "RECORD #: ", ++$ct, "\n"; print "NAME:,$x->{name}\n"; print "AGE:,$x->{age}\n"; print "CITY:,$x->{city}\n"; print " \n"; }
Exercise Perform Exercise #15