0
Hashes and @ARGV Paolo Marcatili - Programmazione 08-09
Agenda <ul><li>Today we will see how to </li></ul><ul><li>Summary of what we’ve done </li></ul><ul><li>Hashes </li></ul><u...
Task Today Paolo Marcatili - Programmazione 08-09
parsing <ul><li>Parse a GO  file </li></ul><ul><li>Extract gene names and function </li></ul>Paolo Marcatili - Programmazi...
Hashes Paolo Marcatili - Programmazione 08-09
Hashes <ul><li>Hashes  are like  array , they store collections of scalars ... but unlike arrays, indexing is by name (jus...
Hashes continued ... <ul><li>Adding a new key-value pair </li></ul><ul><ul><li>$phoneDirectory{“Shirly”} =  7267975 </li><...
Hashes and Foreach <ul><li>Foreach works in hashes as well! </li></ul><ul><ul><li>foreach $person (keys (%phoneDirectory) ...
Hashes and Sorting <ul><li>The sort function works with hashes as well  </li></ul><ul><li>Sorting on the keys </li></ul><u...
Hash and Sorting cont... <ul><li>Sorting by value </li></ul><ul><ul><li>foreach $person (sort {$phoneDirectory{$a} <=> $ph...
Exercise <ul><li>Chose your own test or use  wget  “ http://www.quirinale.it/costituzione/costituzione.htm ” </li></ul><ul...
Counting Words <ul><li>my %seen; </li></ul><ul><li>while (my $l=<F>){ </li></ul><ul><ul><li>my  @w=split (/s+/, $l);# ques...
@ARGV Paolo Marcatili - Programmazione 08-09
Command Line Arguments <ul><li>Command line arguments in Perl are extremely easy. </li></ul><ul><li>@ARGV is the array tha...
Quick Program with @ARGV <ul><li>Simple program called log.pl that takes in a number and prints the log base 2 of that num...
$_ <ul><li>Perl default scalar value that is used when a variable is not explicitly specified. </li></ul><ul><li>Can be us...
$_ and For Loops <ul><li>Example using $_ in a for loop </li></ul><ul><ul><li>@array = ( “Perl”, “C”, “Java” ); </li></ul>...
$_ and File Handlers <ul><li>Example in using $_ when reading in a file; </li></ul><ul><ul><li>while( <> ) { </li></ul></u...
Opendir, readdir Paolo Marcatili - Programmazione 08-09
Opendir & readdir <ul><li>Just like open, but for dirs </li></ul><ul><li># load all files of the &quot;data/&quot; folder ...
Task Paolo Marcatili - Programmazione 08-09
Malaria proteins annotations <ul><li>We’ll do a script that reports all gene with a similar function w.r.t. our query gene...
Upcoming SlideShare
Loading in...5
×

Hashes Master

385

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
385
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Hashes Master"

  1. 1. Hashes and @ARGV Paolo Marcatili - Programmazione 08-09
  2. 2. Agenda <ul><li>Today we will see how to </li></ul><ul><li>Summary of what we’ve done </li></ul><ul><li>Hashes </li></ul><ul><li>Keys </li></ul>Paolo Marcatili - Programmazione 08-09
  3. 3. Task Today Paolo Marcatili - Programmazione 08-09
  4. 4. parsing <ul><li>Parse a GO file </li></ul><ul><li>Extract gene names and function </li></ul>Paolo Marcatili - Programmazione 08-09
  5. 5. Hashes Paolo Marcatili - Programmazione 08-09
  6. 6. Hashes <ul><li>Hashes are like array , they store collections of scalars ... but unlike arrays, indexing is by name (just like in real life!!!) </li></ul><ul><li>Two components to each hash entry: </li></ul><ul><ul><li>Key example : name </li></ul></ul><ul><ul><li>Value example : phone number </li></ul></ul><ul><li>Hashes denoted with % </li></ul><ul><ul><li>Example : %phoneDirectory </li></ul></ul><ul><li>Elements are accessed using {} (like [] in arrays) </li></ul>Paolo Marcatili - Programmazione 08-09
  7. 7. Hashes continued ... <ul><li>Adding a new key-value pair </li></ul><ul><ul><li>$phoneDirectory{“Shirly”} = 7267975 </li></ul></ul><ul><ul><li>Note the $ to specify “scalar” context! </li></ul></ul><ul><li>Each key can have only one value </li></ul><ul><ul><li>$phoneDirectory{“Shirly”} = 7265797 </li></ul></ul><ul><ul><li># overwrites previous assignment </li></ul></ul><ul><li>Multiple keys can have the same value </li></ul><ul><li>Accessing the value of a key </li></ul><ul><ul><li>$phoneNumber =$phoneDirectory{“Shirly”}; </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  8. 8. Hashes and Foreach <ul><li>Foreach works in hashes as well! </li></ul><ul><ul><li>foreach $person (keys (%phoneDirectory) ) </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li>print “$person: $phoneDirectory{$person}”; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Never depend on the order you put key/values in the hash! Perl has its own magic to make hashes amazingly fast!! </li></ul>Paolo Marcatili - Programmazione 08-09
  9. 9. Hashes and Sorting <ul><li>The sort function works with hashes as well </li></ul><ul><li>Sorting on the keys </li></ul><ul><ul><li>foreach $person ( sort keys %phoneDirectory) { </li></ul></ul><ul><ul><li>print “$person : $directory{$person} ”; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>This will print the phoneDirectory hash table in alphabetical order based on the name of the person, i.e. the key. </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  10. 10. Hash and Sorting cont... <ul><li>Sorting by value </li></ul><ul><ul><li>foreach $person (sort {$phoneDirectory{$a} <=> $phoneDirectory{$b}} keys %phoneDirectory) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>print “$person : </li></ul></ul><ul><ul><li>$phoneDirectory{$person} ”; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Prints the person and their phone number in the order of their respective phone numbers, i.e. the value. </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  11. 11. Exercise <ul><li>Chose your own test or use wget “ http://www.quirinale.it/costituzione/costituzione.htm ” </li></ul><ul><li>Identify the 10 most frequent words </li></ul><ul><li>Identify the 10 most frequent words longer than 5 letters </li></ul>Paolo Marcatili - Programmazione 08-09
  12. 12. Counting Words <ul><li>my %seen; </li></ul><ul><li>while (my $l=<F>){ </li></ul><ul><ul><li>my @w=split (/s+/, $l);# questa è una funzione nuova… </li></ul></ul><ul><ul><li>foreach my $word (@w){ </li></ul></ul><ul><ul><li>$word=~s/[sx]$//;#plurial elimination </li></ul></ul><ul><ul><li>$seen { $word } ++; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>foreach my $word (sort {$seen{$a}<=>$seen{$b}} keys %seen){ </li></ul></ul><ul><ul><li>print “Word $word N: $seen{$word} ”; </li></ul></ul><ul><ul><li>} </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  13. 13. @ARGV Paolo Marcatili - Programmazione 08-09
  14. 14. Command Line Arguments <ul><li>Command line arguments in Perl are extremely easy. </li></ul><ul><li>@ARGV is the array that holds all arguments passed in from the command line. </li></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>% ./prog.pl arg1 arg2 arg3 </li></ul></ul></ul><ul><ul><li>@ARGV would contain ('arg1', arg2', 'arg3) </li></ul></ul><ul><li>$#ARGV returns the number of command line arguments that have been passed. </li></ul><ul><ul><li>Remember $#array is the size of the array! </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  15. 15. Quick Program with @ARGV <ul><li>Simple program called log.pl that takes in a number and prints the log base 2 of that number; </li></ul><ul><ul><li>#!/usr/local/bin/perl -w </li></ul></ul><ul><ul><li>$log = log($ARGV[0]) / log(2); </li></ul></ul><ul><ul><li>print “The log base 2 of $ARGV[0] is $log. ”; </li></ul></ul><ul><li>Run the program as follows: </li></ul><ul><ul><li>% log.pl 8 </li></ul></ul><ul><li>This will return the following: </li></ul><ul><ul><li>The log base 2 of 8 is 3. </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  16. 16. $_ <ul><li>Perl default scalar value that is used when a variable is not explicitly specified. </li></ul><ul><li>Can be used in </li></ul><ul><ul><li>For Loops </li></ul></ul><ul><ul><li>File Handling </li></ul></ul><ul><ul><li>Regular Expressions </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  17. 17. $_ and For Loops <ul><li>Example using $_ in a for loop </li></ul><ul><ul><li>@array = ( “Perl”, “C”, “Java” ); </li></ul></ul><ul><ul><li>for(@array) { </li></ul></ul><ul><ul><li>print $_ . “is a language I know ”; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Output : </li></ul></ul><ul><ul><li>Perl is a language I know. </li></ul></ul><ul><ul><li>C is a language I know. </li></ul></ul><ul><ul><li>Java is a language I know. </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  18. 18. $_ and File Handlers <ul><li>Example in using $_ when reading in a file; </li></ul><ul><ul><li>while( <> ) { </li></ul></ul><ul><ul><li>chomp $_; # remove the newline char </li></ul></ul><ul><ul><li>@array = split/ /, $_; # split the line on white space # and stores data in an array </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Note: </li></ul><ul><ul><li>The line read in from the file is automatically store in the default scalar variable $_ </li></ul></ul>Paolo Marcatili - Programmazione 08-09
  19. 19. Opendir, readdir Paolo Marcatili - Programmazione 08-09
  20. 20. Opendir & readdir <ul><li>Just like open, but for dirs </li></ul><ul><li># load all files of the &quot;data/&quot; folder into the @files array </li></ul><ul><li>opendir(DIR, ”$ARGV[0]&quot;); </li></ul><ul><li>@files = readdir(DIR); </li></ul><ul><li>closedir(DIR); </li></ul><ul><li># build a unsorted list from the @files array: </li></ul><ul><li>print &quot;<ul>&quot;; </li></ul><ul><li>foreach $file (@files) { </li></ul><ul><li>next if ($file eq &quot;.&quot; or $file eq &quot;..&quot;); </li></ul><ul><li>print &quot;<li><a href=&quot;$file&quot;>$file</a></li>&quot;; </li></ul><ul><li>} </li></ul><ul><li>print &quot;</ul>&quot;; </li></ul>Paolo Marcatili - Programmazione 08-09
  21. 21. Task Paolo Marcatili - Programmazione 08-09
  22. 22. Malaria proteins annotations <ul><li>We’ll do a script that reports all gene with a similar function w.r.t. our query gene. </li></ul><ul><li>http://www. biocomputing . it/master_hashes </li></ul>Paolo Marcatili - Programmazione 08-09
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×