CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
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.
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.
Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong. Administrivia Homework 1 graded – you should have gotten an from me.
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
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 Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Perl Hashes Software Tools. Slide 2 “bill” “cheap” What is a Hash? l A hash (or associative array) is like an array, where the index can be any scalar.
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.
17-Jun-15 Assorted Ruby Details. The command line irb starts an interactive Ruby interpreter ruby starts Ruby, with input from the command line End with.
Perl Hashes Learning Objectives: 1. To understand the concept of Hash as a data structure 2. To learn the operations of Hash which are available in Perl.
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,
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Perl hashes, compound data structures, formatting output, and special variables.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Perl Hashes Learning Objectives: 1. To understand the concept of Hash as a data structure 2. To learn the operations of Hash which are available in Perl.
Guide To UNIX Using Linux Third Edition
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
Practical Extraction & Report Language PERL Joseph Beltran.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
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.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
File IO and command line input CSE 2451 Rong Shi.
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.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Introduction to Unix – CS 21
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
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.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
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.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
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
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.
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.
Input from STDIN STDIN, standard input, comes from the keyboard.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Perl hashes, compound data structures, formatting output, and special variables.
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 I/O Learning Objectives:
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O

Hash Variables A hash is a collection of scalar data, selected by arbitrary scalars called keys Hash elements are ordered internally to facilitate access (don’t try to control this order) Hashes resemble Rolodex filing cards whose indexes are similar to hash keys, and the card entries similar to the hash value

Accessing Hashes Hash elements are separate scalar variables, accessed by a string index (the key) Curly brackets are used to enclose the key: $fred{“aaa”} = “bbb”;# key “aaa”, value “bbb” $fred{234.5} = 456.7;# key “234.5”, value print $fred{“aaa”};# prints “bbb” $fred{234.5} += 3;# becomes 459.7

Un/winding Hashes Unwinding a hash moves key-value pairs into an array or a copy of a = %fred;# unwind %fred into # Winding creates a new hash from an existing array or copy of a hash %barney create %barney from # %barney = %fred;# faster winding method

More Hash Windings... %smooth = (“aaa”,“bbb”,“234.5”,456.7); # create %smooth from literal values %backwards = reverse %normal; # swap keys and values; beware of identical values, # which after swapping become non-unique keys

Hash Functions The following functions are used to process hashes: –Keys –Values –Each –Delete

Keys function In list context, keys ($hashname) yields a list of all current keys in a hash %hashname In scalar context, keys ($hashname) yields the number of elements (key-value pairs) in the hash

Examples using keys function $fred{“aaa”} = “bbb”; $fred{234.5} = = gets (“aaa”, 234.5) # or (234.5, “aaa”) due to # arbitrary ordering foreach $key (keys (%fred))# process each $key in list {# generated by keys function print “At $key we have $fred{$key}\n”: # print key and value pair } if (keys(%somehash)) { # if keys() not zero, %somehash is not empty } if (%somehash) { # if True (not zero), %somehash is not empty }

Values Function The values %hashname function yields a list of all current values in %hashname %lastname = ();# force %lastname empty $lastname{“fred”} = “flinstone”; $lastname{“barney”} = = may be # (“flinstone”, “rubble”) or # (“rubble”,”flinstone”) due # to arbitrary ordering

Each function The each %hashname function returns a key-value pair for each element of %hashname while (($first, $last) = each(%lastname)) {# process each %lastname element # $first is key, $last is value print “The last name of $first is $last\n”; # print key and value pair }

Delete function The delete %hashname{“key”} function removes the key-value pair from %hashname %fred = (“aaa”,”bbb”,234.5,34.56); # %fred has two elements delete $fred{“aaa”}; # %fred is now (234.5, 34.56)

Hash Slices Using slices, two or more hash elements can be accessed at a time Hash slices can be used with variable interpolation Hash slices can also be used to merge a smaller hash into a larger one

Examples of Hash Slices # doing this: $score{“fred”} = 205; $score{“barney”} = 195; $score{“dino”} = 30; # is same as: ($score{“fred”},$score{“barney”},$score{“dino”}) = (205,195,30); # and same = (205,195,30); # slices and variable = qw(fred barney dino); print “scores are:

More hash slice examples # Merging a smaller hash into a larger one # Get values of %score, and copy them into the # %league hash whenever the keys of %score match # the keys of %league %league{keys %score} = values %score; # equivalent to %league = (%league, %score);

Input from STDIN can be used to read data in both list and scalar contexts In list = ; reads lines until end of file (or CTRL-D) In scalar context, $a = ; reads input up to newline, or whatever $/ is set to also accepts the $_ scalar variable: $_ = ;

Example reading STDIN to $_ # The following code… while (defined ($line = )) { chomp $line; print $line; } # …is same as: while ( )# or while ($_ = ) { chomp;# or chomp $_; print;# or print $_; }

Input from the <> operator The <> operator behaves like, but takes data from file(s) specified by array By default, the element(s) contain command-line arguments (filenames specified at the can be redefined in a program

Example of input from <> #!/usr/bin/perl # this program (kitty) works like UNIX cat # invoke with kitty file1 file2 file3 # the following lists the contents of this file: # kitty kitty.pl # while (<>)# read files from command-line { print $_;# print output }

Example #!/usr/bin/perl # this program (kitty) works like UNIX cat # and lists contents of files “aaa”, “bbb”, # and “ccc” = (“aaa”,“bbb”,“ccc”); while (<>)# read files from command-line { print $_;# print output }

Output to STDOUT PERL can use print or printf functions to produce output The print function returns 1, unless an I/O error occurs The printf function allows greater degree of control, and is similar to the printf function in C

STDOUT examples $a = print(“Hello ”, “world”, “\n”); # $a is set to 1 when print succeeds printf “%15s %5d %10.2f\n”, $s, $n, $r # prints $s as a 15 character string field # prints a space # prints $n as a 5 character decimal integer # prints a space # prints $r as a 10 character 2 decimal place # floating point field # prints a newline