5, "bob"=>"zzz", 50=>"John"); Accessing: you can access a value by its key: print $hash{50};John 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 Hash – an associative array %hash 5"a" => "zzz""bob" => "John"50 =>">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

9.1 Hashes. 9.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Ofir Enter a phone number: 08-8617262 Enter.

Similar presentations


Presentation on theme: "9.1 Hashes. 9.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Ofir Enter a phone number: 08-8617262 Enter."— Presentation transcript:

1 9.1 Hashes

2 9.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Ofir Enter a phone number: 08-8617262 Enter a name that will be added to the phone book: Dudu Enter a phone number: 03-6409245 Hash Motivation

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

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

5 9.5 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 =>

6 9.6 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 %hash 5"a" => "zzz""bob" => "John"50 => @hashVals 5 "zzz" "John" @hashKeys "a" "bob" 50

7 9.7 To iterate over all the values in %hash foreach $value (values(%hash))... To iterate over the keys in %hash foreach $key (keys(%hash))... For example: foreach $key (keys(%hash)) { 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

8 9.8 Notably: The elements are given in an arbitrary order, so if you want a certain order use sort: foreach $key (sort(keys(%hash))){... } Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 => @hashVals 5 "zzz" "John" @hashKeys "a" "bob" 50

9 9.9 Let’s see how to use a hash as a phone book… (phoneBook.pl) Example

10 9.10 Class exercise 9a 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. 2.Add to question 1: 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 … )

11 9.11 You can use combinations of hashes (and arrays) together to construct more complex data structures. If the information is best represented in two levels it is useful to use a hash within a hash: my %hash; $hash{Key_level_1}{Key_level_2}; Hash within Hash

12 9.12 For example: for each name in the phone book, we want to store both the phone number and the address: # %phoneBook includes names, each name has both a phone number # and an address my %phoneBook; $phoneBook{'Dudu'}{'Phone'} = "09-9545995"; $phoneBook{'Dudu'}{'Address'} = "115 Menora St., Hulun"; $phoneBook{'Ofir'}{'Phone'} = "054-4898799"; $phoneBook{'Ofir'}{'Address'} = "31 Horkanus St., Eilat"; Hash within Hash

13 9.13 Class exercise 9b 1. Write a script that reads a file with a list of protein names, lengths and location: AP_000081181Nuc AP_000174104Cyt AP_000138145Cyt stores the names of the sequences as hash keys, and use "length" and "location" as keys within each protein. For example: $proteins{AP_000081}{"length"} should equal 181 $proteins{AP_000081}{"location"} should equal "Nuc" 2. Use the phoneBook.pl example and change it such that for each name in the phone book, the user enters the following data:phoneBook.pl » Phone number » Address » ID number In the input section: ask for a name and it's corresponding phone, address and ID. In the retrieval section: ask for a name and the specific required data.


Download ppt "9.1 Hashes. 9.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Ofir Enter a phone number: 08-8617262 Enter."

Similar presentations


Ads by Google