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.

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 The Basics of Perl.
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.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an from me.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
COS 381 Day 19. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
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.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
Programming with Collections Collections in Java Using Arrays Week 9.
ISBN Chapter 6 Data Types: Structured types.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
 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.
The Ruby Programming Language
Lecture 12 PHP Basics Boriana Koleva Room: C54
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
Lists in Python.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Continuing with Perl Homework 3: first Perl homework – due Sunday by midnight – one PDF file, by .
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
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.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Perl Practical(?)‏ Extraction and Report Language.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Arrays “Array, I'm bound array, 'cross the wide Missouri.” ‒ Old Programmer’s Folk Song © Copyright 2014, Fred McClurg All Rights Reserved.
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy 1 Day 3: Collections suggested reading: Learning Perl (4th Ed.), Chapter 3:
Built-in Data Structures in Python An Introduction.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
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.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong. Administrivia Homework 2 graded.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 The Basics of Perl.
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”;
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CS 105 Perl: Basic I/O, Context, Strings, Lists Nathan Clement January 22, 2014.
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.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Topic 3: Lists and arrays CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 3, pages 40-55, 86-91, Programming Perl 3rd edition.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Interpolation Variable Interpolation, Backslash Interpolation.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Definition of the Programming Language CPRL
5 Day Forecast Mon Tues Wed Thu Fri.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Introduction to Perl Jarrad Battaglia.
Object Oriented Programming in java
CS2011 Introduction to Programming I Arrays (I)
Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
Presentation transcript:

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

Structure of Hashes Hashes are lists of scalar values BUT have string indices (called keys) keys also stored in structure variable name starts with % have their own namespace (like arrays) need not be declared, grow and shrink no way to determine order internal has function to store and retrieve

literals no hash literals (use list literals) (“bob”, 42, “carol”, 40, …) or use => instead of comma (“bob” => 42, “carol” => 40, …) or (bob => 42, carol => 40, …) – do not need “ “’s, left of => implicitly quotes barewords

first is actually a list, odd subscripted elements of array  keys of the = (Bob, 42, Carol, 40); %ages same as %ages =(“bob” => 42, “carol” => 40); must be even length! %salaries = (“Bob” => 79_500, “Carol” => 43_000);

accessed by “subscripting” with key $salaries{“Bob”}  insert new values $salaries{“Mike”} = 51_950; if Mike not in table, adds it if Mike is in table, changes value set to empty %salaries = (); undef %salaries NOT %salaries = undef;  (1 element, undef)

printing hash variables not interpolated in double- quoted strings print “%salaries\n”; – prints %salaries print %salaries; – prints keys and values, no spaces

slice of hash gives us a list or  (79500, 51950) note form of the variable since slice of a hash is an array, can be interpolated in double quoted strings

operators delete $salaries {“Billie”}; – key and salary deleted from %salaries if (exists $salaries{“Billie”}) … – to find out if in hash

keys and values operators keys and values of a hash are arrays keys operator  list of keys values operator  list of values %highs=(“mon”=>64, “tue”=>66, “wed”=>72, “thu”=>55, = keys %highs; #array is (“mon”, “tue”, “wed”, “thu”, “fri”)

foreach foreach $day { … } or foreach $day (keys %highs){ print “on $day,the temp was $highs{$day}\n”; } ^hashing of course, can sort (sort (keys %highs)) keys in scalar context $length = keys %highs;

values = values %highs; foreach $temp (values %highs){ print “$tep\n”; }

Process pairs use each operator to return next element ($day, $temp) = each %highs; usually iterate on it while (($day, $temp)= each %highs){ print “On $day, the high temp was $temp.\n”; } cannot add to hash in loop body, if keys or each used in loop

in boolean expression if (%highs) … scalar context –> boolean expression, true if hash not empty

Predefined hashes – %ENV in first example When to use array vs. hash – when you have many accesses to specific elements

Examples freq.pl FindFiles.pl