Download presentation
Presentation is loading. Please wait.
1
Scripting Languages Chapter 5 Hashes
2
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
3
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
4
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
5
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, );
6
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.
7
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
8
#!/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”;
9
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 @allthekeys = keys(%accounts); @allthevalues = values(%accounts); delete function – delete $accounts{Mike}; grow a hash simply by adding to it: $accounts{Maria} = 100; $accounts{Dave} = 100;
10
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”; }
11
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”;
12
exists Function to see whether a key exists – returns a true value if it exists if (exists $books {“Tolkien”}) { print “Book found \n”; }
13
#!/usr/bin/perl -w # This program assigns an array to a hash # and displays the keys and the values # of the hash # create an array @array = qw( one 1 two 2 three 3 four 4 five 5 ); print "\nThe array is: @array\n"; # 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" }
14
#!/usr/bin/perl -w sub create { my($name, $age, $city) = @_; my $emp = { name => $name, age => $age, city => $city }; return $emp; } while ( ) { @info = split; $record = create(@info); push(@list, $record); } while(@list) { $x = pop(@list); print "RECORD #: ", ++$ct, "\n"; print "NAME:,$x->{name}\n"; print "AGE:,$x->{age}\n"; print "CITY:,$x->{city}\n"; print "--------------------\n"; }
15
Exercise Perform Exercise #15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.