Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.

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

Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
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.
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an from me.
Python Dictionary.
Chapter 10.
Stacks.
VBA Modules, Functions, Variables, and Constants
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
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.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
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.
Guide To UNIX Using Linux Third Edition
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
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.
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Nael Alian Introduction to PHP
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Prof. Alfred J Bird, Ph.D., NBCT -bird.wikispaces.umb.edu/ Office – McCormick 3rd floor.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Arrays. Indexed Arrays Use numbers as keys: $grades = array(); $grades[0] = ' A ' ; $grades[1] = ' A- ' ; $grades[2] = ' B+ ' ; Or with one statement:
Built-in Data Structures in Python An Introduction.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
WHAT IS A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
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.
Introduction to Unix – CS 21
5 1 Data Files CGI/Perl Programming By Diane Zak.
Copyright Curt Hill Variables What are they? Why do we need them?
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
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.
CS 105 Perl: Basic I/O, Context, Strings, Lists Nathan Clement January 22, 2014.
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
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.
Perl Day 5. Arrays vs Hash Arrays are one way to store multiple things in a variable. Hashes are another. Arrays are one way to store multiple things.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
Unit 6 Repetition Processing Instructor: Brent Presley.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
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.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
References and Data Structures
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Perl Variables: Array Web Programming.
CHAPTER THREE Sequences.
Perl Variables: Hash Web Programming.
Perl I/O Learning Objectives:
Presentation transcript:

Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates a key with a value. A hash is composed of a set of key-value pairs. A key is a string: any collection of characters, generally enclosed in quotes. Any scalar can be a key, but they are all converted to strings. A value can be almost anything: the values are just scalar variables. One hash oddity: neither the keys nor the values is sorted or stored in a useful order. The order you enter hash items is not related to the order with which you retrieve them.

Why Use Hashes? The C language doesn’t have anything like a hash in it, and clearly C can do just about anything you need to do in programming. The point of Perl is to make your life easier, to include useful tools, even if they are messy and clutter up the language. Examples of hash usage: --keeping count of the number of times a word is used in a text, or that a particular sub-sequence appears in DNA. Use the word as the key and the number of appearances as the value. --Associating ID numbers with people’s names --Associating protein names with their properties. And lots more. The hash is a tool that gets used very frequently once you understand them.

Hash Basics The punctuation mark used to denote a hash is % (percent sign). Note that hashes, arrays, and scalars are completely different variables. The variables and %cat are all different and independent variables. I don’t recommend using the same names for different variables, but it is legal. Hash elements are accessed by enclosing the key in curly braces. For example, the hash %stoplight is populated as follows: $stoplight{red} = “stop”; $stoplight{yellow} = “caution”; $stoplight{green} = “go”; In this hash, “red”, “yellow”, and “green” are the keys, and “stop”, “caution”, and “go” are the values. Each key can refer to only a single value. You can’t have duplicate keys. If you try, the first value will be lost and only the second will work: $stoplight{yellow} = “speed up”; print “$stoplight{yellow}\n”; # prints “go faster” However, different keys can refer to the same value without any problem.

Alternative Way of Loading Hashes A hash really is an array with alternating keys and values. You could load a hash by simply writing the keys and values the same way as you would load an array: %stoplight = (“red”, “stop”, “yellow”, “caution”, “green”, “go”); This method is a bit annoying, because it is easy to lose track of keys and values. A better way is to use the => operator (“big arrow”), which is really just a synonym for a comma: %stoplight = ( “red” => “stop”, “yellow” => “caution”, “green” => “go” ); Note the use of newlines here--makes reading the code easier.

Hash Operators “keys” gives a list of all the keys used in the hash. Here’s a common use: foreach $key (keys %stoplight) { print “$key stands for $stoplight{$key}\n”; } Note that the keys are not returned in a useful order. If you want them sorted you could write: foreach (sort keys %stoplight) { or foreach (sort {$a $b} keys %stoplight) { Similarly, “values” gives a list of all the values, in some unusual order. “each” is an operator that returns a 2 element list consisting of the key and the value. It needs to do this in tandem with “while”: while ( ($key, $value) = each %stoplight) { print “$key : $value\n”; }

More Hash Operators Removing elements in a hash is done with “delete”: delete $stoplight{“red”}; # both key and value are removed Testing for existence with “exists”: exists $stoplight{“red”) returns true if that key-value pair exists, and “false” if it doesn’t.

Counting Here’s a simple use of hashes to get word counts. Input is a file of words, one per line. while ($word = ) { $chomp $word; $word_hash{$word}++; } Note that %word_hash was created implicitly, without ever being explicitly declared. This is a standard Perl feature, but I will shortly discourage its use. Also, a nice feature of using hashes for counting is that each value is automatically set to 1 the first time a new key is encountered. That is, you don’t have to initialize each key-value pair; Perl does it for you automatically.