Presentation is loading. Please wait.

Presentation is loading. Please wait.

7ex.1 Hashes. 7ex.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Eyal Enter a phone number: 08-8617262.

Similar presentations


Presentation on theme: "7ex.1 Hashes. 7ex.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Eyal Enter a phone number: 08-8617262."— Presentation transcript:

1 7ex.1 Hashes

2 7ex.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Eyal 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 7ex.3 Let's say we want to create a phone book... Enter a name that will be search for in the phone book: Eyal The phone number of Eyal is: 08-8617262 Hash Motivation

4 7ex.4 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated with a value. A hash variable name always start with a “%”: my %h = ("a"=>5, "bob"=>"zzz", 50=>"Johnny"); You can access a value by its key: print $h{50}.$h{a};Johnny5 $h{bob} = "aaa"; (modifying an existing value) $h{555} = "z"; (adding a new key-value pair) Hash – an associative array

5 7ex.5 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated with a value. A hash variable name always start with a “%”: my %h = ("a"=>5, "bob"=>"zzz", 50=>"Johnny"); You can ask whether a certain key exists in a hash: if (exists($h{50}))... You can delete a certain key-value pair in a hash: delete($h{50}); Hash – an associative array

6 7ex.6 To iterate over the values in %h foreach $value (values(%h))... To iterate over the keys in %h foreach $key (keys(%h))... For example: foreach $key (keys(%h)) { print "The key is $key\n"; print "The value is $h{$key}\n"; } The elements are given in an arbitrary order, so if you want a certain order use sort: foreach $key (sort(keys(%h)))... These functions actually return a list of values, so you could: @keys = keys(%h); Iterating over hash elements

7 7ex.7 my %h = ("a"=>5, "bob"=>"zzz", 50=>"Johnny"); In addition you can use: while(@pair = each(%h)){ print "key is $pair[0], value is $pair[1]\n" } to get an array of a key-value pair in each iteration (e.g. ("a",5) ) Iterating over hash elements

8 7ex.8 Let’s see how to use a hash as a phone book… (phoneBook.pl) Example

9 7ex.9 Class exercise 9 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 the script: Read another file, and print the names that appeared in both files with the same length. Give a warning if the name is the same but the length is different. 3. Write a script that reads a GenPept file, 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 … )


Download ppt "7ex.1 Hashes. 7ex.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Eyal Enter a phone number: 08-8617262."

Similar presentations


Ads by Google