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.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

Chapter 25 Perl and CGI (Common Gateway Interface)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Skip List & Hashing CSE, POSTECH.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
VBA Modules, Functions, Variables, and Constants
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
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:
for($i=0; $i/)
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
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.
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.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
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
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Introduction to Awk Awk is a convenient and expressive programming language that can be applied to a wide variety of computing and data manipulation tasks.
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.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Built-in Data Structures in Python An Introduction.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
An Intro to Perl, Pt. 2 Hashes, Foreach Control, and the Split Function.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Basics. sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”;
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.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
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),
1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern.
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.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 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.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
10.1Retrieving a Database File – In the last chapter, we have created a database file and several tables for that database. – In order to view and modify.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
By Joseph Patrick Brady. Overview  Hashes  foreach control statement  split function.
Web Database Programming Using PHP
Web Database Programming Using PHP
Chapter 5 - Control Structures: Part 2
Perl Variables: Array Web Programming.
Introduction to Perl Jarrad Battaglia.
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
Presentation transcript:

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 an indices Hash indices are called keys – not numbers but arbitrary strings

Keys Are strings – instead of accessing element number 3 we’ll access the hash element named Monday You can use any string expression for a hash key – just as there’s only one array element 3 – there’s only one hash element - Monday

Associative Array Hash = = Associative Array Key - > Value Key is used to receive the value This data type is used to implement table lookups. Key is usually string data – thus lookup capital city by its state or lookup bank balance by the name of the account

Associative Array’s Cont’d They save the programmer the actual programming of the lookup Define a hash with % then pairs must be specified. %accounts = (Mike => 100, Sue => 200, Erin => 150, Patti => 250, );

Associative Array’s Cont’d  Is the symbol used to make the associations clear  First two entries represent first pair …… %accounts = (Mike,100,Sue,200,Erin,150,Patti,250); An associative array and a regular array may have the same name as they exist in their own namespaces.

Hashes Cont’d You must use { } to extract a particular value $accounts{Mike} #yields 100 $accounts{Sue} # yields 200 The key that acts as the subscript may be quoted or not – if the key contains embedded blanks, then the key must be quoted

#!/usr/bin/perl –w # hash.pl %accounts = (Mike => 100, Sue => 200, Erin => 150, Patti => 250 ); print “Enter a name “; $name = ; #read a name chomp ($name);#remove newline character print “$name has balance: $accounts{$name}\n”;

Hash Functions besides using a key to retrieve a value, a common activity on hashes is to get a list of all the keys or a list of all the values keys & = = values(%accounts); delete function – delete $accounts{Mike}; grow a hash simply by adding to it: $accounts{Maria} = 100; $accounts{Dave} = 100;

More Hash Functions each functions – allows you to iterate over an entire hash – returns a key – value pair as a two element list. while ( ($key, $value) = each %hash) { print “$key => $value \n”; }

sort function sorts your has so that key value displays in a sorted fashion foreach $key ( sort keys %hash) { $value = $hash($key); print “$key => $value \n”;

exists Function to see whether a key exists – returns a true value if it exists if (exists $books {“Tolkien”}) { print “Book found \n”; }

#!/usr/bin/perl -w # This program assigns an array to a hash # and displays the keys and the values # of the hash # create an = qw( one 1 two 2 three 3 four 4 five 5 ); print "\nThe array # A loop to assign array to a hash for( $key=0; $key<10; $key +=2 ) { $hash{$array[$key]}=$array[$key + 1]; } # A loop to print the hash while (($key, $value) = each(%hash)) { print "$key => $value \n" }

#!/usr/bin/perl -w sub create { my($name, $age, $city) my $emp = { name => $name, age => $age, city => $city }; return $emp; } while ( ) = split; $record = $record); } { $x = print "RECORD #: ", ++$ct, "\n"; print "NAME:,$x->{name}\n"; print "AGE:,$x->{age}\n"; print "CITY:,$x->{city}\n"; print " \n"; }

Exercise Perform Exercise #15