160 likes | 313 Views
Hashes (associative arrays). Hash Motivation. Let's say we want to create a phone book . . . Enter a name that will be added to the phone book: Dudi Enter a phone number: 6409245 Enter a name that will be added to the phone book: Dudu Enter a phone number: 6407693. %hash. =>. "a". 5.
E N D
Hash Motivation Let's say we want to create a phone book . . . Enter a name that will be added to the phone book: Dudi Enter a phone number: 6409245 Enter a name that will be added to the phone book: Dudu Enter a phone number: 6407693
%hash => "a" 5 => "bob" "zzz" => 50 "John" Hash – an associative array An associative array (or simply – a hash) is an unordered set ofpairs of keys and values. Each key is associated with a value. A hash variable name always start with a “%”: my %hash; Initialization: %hash = ("a"=>5, "bob"=>"zzz", 50=>"John"); Accessing: you can access a value by its key: print $hash{50}; John Tip you can reset the hash (to an empty one) by %hash = ();
%hash %hash %hash %hash => => => => "a" "a" "a" "a" 5 5 5 5 => => 555 "z" 50 "John" => => => => "bob" "bob" "bob" "bob" "aaa" "aaa" "aaa" "zzz" => 555 "z" => => 50 50 "John" "John" Hash – an associative array modifying : $hash{bob} = "aaa"; (modifying an existing value) adding : $hash{555} = "z"; (adding a new key-value pair) You can ask whether a certain key exists in a hash: if (exists $hash{50} )... You can delete a certain key-value pair in a hash:delete($hash{50});
$hash{key} $number-3.54 %hash @array => $string"hi\n" => $array[0] => Variable types in PERL Scalar Array Hash
%hash => "Dudi" 9245 => "Dudu" 7693 Hash – an associative array An associative array of the phone book suggested in the first slide(we will se a more elaborated version later on): Declare my %phoneBook; Updating $phoneBook{"Dudi"} = 9245; $phoneBook{"Dudu"} = 7693; Fetching print $phoneBook{"Dudi"};
%hash => "a" 5 => "bob" "zzz" => 50 "John" @hashVals @hashKeys "zzz" "bob" 5 "a" 50 "John" Iterating over hash elements It is possible to get a list of all the keys in %hashmy @hashKeys= keys(%hash); Similarly you can get an array of the values in %hashmy @hashVals= values(%hash);
%hash => "a" 5 => "bob" "zzz" => 50 "John" @hashVals @hashKeys "bob" "zzz" "a" 5 50 "John" Iterating over hash elements To iterate over all the values in %hash my @hashVals = values(%hash); foreach my $value (@hashVals)... To iterate over the keys in %hashmy @hashKeys = keys(%hash); foreach my $key (@hashKeys)...
%hash => "a" 5 => "bob" "zzz" => 50 "John" @hashVals @hashKeys "bob" "zzz" "a" 5 50 "John" Iterating over hash elements For example, iterating over the keys in %hash : my @hashKeys = keys(%hash); foreach my $key (@hashKeys) { print "The key is $key\n"; print "The value is $hash{$key}\n"; } The key is a The value is 5 The key is bob The value is zzz The key is 50 The value is John
%hash => "a" 5 => "bob" "zzz" => 50 "John" @hashVals @hashKeys "bob" "zzz" "a" 5 50 "John" Iterating over hash elements Notably: The elements are given in an arbitrary order,so if you want a certain order use sort: my @hashKeys= keys(%hash); my @sortedHashKeys= sort(@hashKeys); foreach$key (@sortedHashKeys) { print "The key is $key\n"; print "The value is $hash{$key}\n"; }
Example – phoneBook.pl #1 ###################################### # Purpose: Store names and phone numbers in a hash, # and allow the user to ask for the number of a certain name. # Input: Enter name-number pairs, enter "END" as a name to stop, # then enter a name to get his/her number # use strict; my%phoneNumbers= (); my $number;
Example – phoneBook.pl #2 # Ask user for names and numbers and store in a hash my $name = ""; while ($name ne "END") { print "Enter a name that will be added to the phone book:\n"; $name = <STDIN>; chomp $name; if ($nameeq "END") { last; } print "Enter a phone number: \n"; $number = <STDIN>; chomp $number; $phoneNumbers{$name} = $number; }
Example – phoneBook.pl #3 # Ask for a name and print the corresponding number $name = ""; while ($name ne "END") { print"Enter a name to search for in the phone book:\n"; $name = <STDIN>; chomp $name; if (exists($phoneNumbers{$name})) { print "The phone number of $name is:$phoneNumbers{$name}\n"; } elsif ($name eq "END") { last; } else { print "Name not found in the book\n"; } }
Class exercise 8 • Write a script that reads a file with a list of protein names and lengths(proteinLengths ):AP_000081 181AP_000174 104AP_000138 145stores the names of the sequences as hash keys, with the length of the sequence as the value. Print the keys of the hash. • Add to Q1: Read another file, and print the names that appeared in both files with the same length. Print a warning if the name is the same but the length is different. • Write a script that reads a GenPept file (you may use the preproinsulin record), finds all JOURNAL lines, and stores in a hash the journal name (as key) and year of publication (as value): • a. Store only one year value for each journal name • b*. Store all years for each journal name • Then print the names and years, sorted by the journal name (no need to sort the years for the same journal in b*, unless you really want to do so…)