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.

Slides:



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

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Programming and Perl for Bioinformatics Part III.
COMPSCI 101 Principles of Programming Lecture 24 – Using dictionaries to manage a small file of information.
10.1 References & Complex Data Structures Variable types in PERL ScalarArrayHash $number $string %hash $reference
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.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
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}
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.
14.1 Wrapping up Revision 14.3 References are your friends…
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.
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.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Database Software Application
Advanced Perl for Bioinformatics Lecture 5. Regular expressions - review You can put the pattern you want to match between //, bind the pattern to the.
1 Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
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
The Fun That Is File Structures Pages By: Christine Zeitschel.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
File Processing - Indexing MVNC1 Indexing Jim Skon.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
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.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
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.
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
ASP.Net, Web Forms and Web Controls 1 Outline Session Tracking Cookies Session Tracking with HttpSessionState.
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.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
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),
Lesson 13 Databases Unit 2—Using the Computer. Computer Concepts BASICS - 22 Objectives Define the purpose and function of database software. Identify.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Maps Nick Mouriski.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
CSE 143 Lecture 11: Sets and Maps reading:
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
3.5 Perform Basic Matrix Operations Add Matrices Subtract Matrices Solve Matric equations for x and y.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Database (Microsoft Access). Database A database is an organized collection of related data about a specific topic or purpose. Examples of databases include:
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Introduction to Perl Jarrad Battaglia.
Perl Variables: Hash Web Programming.
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
CS 1111 Introduction to Programming Fall 2018
Presentation transcript:

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

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

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

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

9.6 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 elements %hash 5"a" => "zzz""bob" => "John"50 5 "zzz" "a" "bob" 50

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 5 "zzz" "a" "bob" 50

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 5 "zzz" "a" "bob" 50

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

9.10 Class exercise 9a 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. 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 … )

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

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'} = " "; $phoneBook{'Dudu'}{'Address'} = "115 Menora St., Hulun"; $phoneBook{'Ofir'}{'Phone'} = " "; $phoneBook{'Ofir'}{'Address'} = "31 Horkanus St., Eilat"; Hash within Hash

9.13 Class exercise 9b 1. Write a script that reads a file with a list of protein names, lengths and location: AP_ Nuc AP_ Cyt AP_ Cyt 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.