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 – an associative array %hash 5"a" => "zzz""bob" => "John"50 =>"> 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 – an associative array %hash 5"a" => "zzz""bob" => "John"50 =>">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

8.1 Hashes (associative arrays). 8.2 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.

Similar presentations


Presentation on theme: "8.1 Hashes (associative arrays). 8.2 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."— Presentation transcript:

1 8.1 Hashes (associative arrays)

2 8.2 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 Motivation

3 8.3 An associative array (or simply – a hash) is an unordered set of pairs 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 – an associative array %hash 5"a" => "zzz""bob" => "John"50 =>

4 8.4 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 – an associative array %hash 5"a" => "zzz""bob" => "John"50 => %hash 5"a" => "aaa""bob" => "John"50 => %hash 5"a" => "aaa""bob" => "John"50 => "z"555 => %hash 5"a" => "aaa""bob" => "z"555 =>

5 8.5 Variable types in PERL ScalarArrayHash $number -3.54 $string "hi\n" @array %hash => $array[0] $hash{key}

6 8.6 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 – an associative array %hash 9245"Dudi" => 7693"Dudu" =>

7 8.7 It is possible to get a list of all the keys in %hash my @hashKeys = keys(%hash); Similarly you can get an array of the values in %hash my @hashVals = values(%hash); Iterating over hash elements @hashVals 5 "zzz" "John" @hashKeys "a" "bob" 50 %hash 5"a" => "zzz""bob" => "John"50 =>

8 8.8 To iterate over all the values in %hash my @hashVals = values(%hash); foreach my $value (@hashVals)... To iterate over the keys in %hash my @hashKeys = keys(%hash); foreach my $key (@hashKeys)... Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 => @hashVals 5 "zzz" "John" @hashKeys "a" "bob" 50

9 8.9 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 Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 => @hashVals 5 "zzz" "John" @hashKeys "a" "bob" 50

10 8.10 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"; } Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 => @hashVals 5 "zzz" "John" @hashKeys "a" "bob" 50

11 8.11 ###################################### # 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 #1

12 8.12 # 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 = ; chomp $name; if ($name eq "END") { last; } print "Enter a phone number: \n"; $number = ; chomp $number; $phoneNumbers{$name} = $number; } Example – phoneBook.pl #2

13 8.13 # 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 = ; 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"; } Example – phoneBook.pl #3

14 8.14 Class exercise 8 1.Write a script that reads a file with a list of protein names and lengths: AP_000081 181 AP_000174 104 AP_000138 145 stores the names of the sequences as hash keys, with the length of the sequence as the value. Print the keys of the hash. 2.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. 3.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):the preproinsulin record 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 … )


Download ppt "8.1 Hashes (associative arrays). 8.2 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."

Similar presentations


Ads by Google