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.

Slides:



Advertisements
Similar presentations
A primer on Perl programming First structures (with examples)
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Programming and Perl for Bioinformatics Part III.
11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}
Scripting Languages Chapter 5 Hashes. Hash Data structure, not unlike an array – it will hold any number of values It indexes values by name – not by.
Advanced Perl for Bioinformatics Lecture 5. Regular expressions - review You can put the pattern you want to match between //, bind the pattern to the.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
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:
9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
for($i=0; $i/)
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
8.1 References and complex data structures. 8.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
CSC3530 Software Technology Tutorial Two PERL Basics.
10.1 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
8ex.1 References and complex data structures. 8ex.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
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: Enter.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Advanced Perl for Bioinformatics Lecture 5. Regular expressions - review You can put the pattern you want to match between //, bind the pattern to the.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
Computer Programming for Biologists Class 7 Nov 27 th, 2014 Karsten Hokamp
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
9 1 DBM Databases CGI/Perl Programming By Diane Zak.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Prof. Alfred J Bird, Ph.D., NBCT -bird.wikispaces.umb.edu/ Office – McCormick 3rd floor.
Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
Introduction to Perl Giorgos Georgakilas Graduated from C.E.I.D.Graduated from C.E.I.D. M.Sc. degree in ITMBM.Sc. degree in ITMB Ph.D. student in DIANA-LabPh.D.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
8.1 Common Errors – Exercise #3 Assuming something on the variable part of the input file. When parsing a format file (genebank, fasta or any other format),
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
Maps Nick Mouriski.
C++ Review STL CONTAINERS.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
CSI605 perl. Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
Introduction to Perl Jarrad Battaglia.
Perl Variables: Hash Web Programming.
Control Structures: for & while Loops
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
Recitation Outline C++ STL associative containers Examples
Remembering lists of values lists
Presentation transcript:

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 number: Enter a name that will be added to the phone book: Dudu Enter a phone number: Hash Motivation

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

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

8.5 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}

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" =>

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

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

8.9 For example, iterating over the keys in %hash : = keys(%hash); foreach my $key { 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 5 "zzz" "a" "bob" 50

8.10 Notably: The elements are given in an arbitrary order, so if you want a certain order use sort: = keys(%hash); = foreach $key { print "The key is $key\n"; print "The value is $hash{$key}\n"; } Iterating over hash elements %hash 5"a" => "zzz""bob" => "John"50 5 "zzz" "a" "bob" 50

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

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

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

8.14 Class exercise 8 1.Write a script that reads a file with a list of protein names and lengths: AP_ AP_ AP_ 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 … )