Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.

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

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
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.
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.
Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
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 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.
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
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.
CS 898N – Advanced World Wide Web Technologies Lecture 7: PERL Chin-Chih Chang
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Guide To UNIX Using Linux Third Edition
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
Computer Programming for Biologists Class 7 Nov 27 th, 2014 Karsten Hokamp
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.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
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
– Introduction to Perl 10/20/ Introduction to Perl - Lists and Arrays Introduction to Perl Session 3 · lists and arrays.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
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.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
5 1 Data Files CGI/Perl Programming By Diane Zak.
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
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.
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
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)
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 Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
Working with Loops, Conditional Statements, and Arrays.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
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.
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
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.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
IT441 Network Services Administration
Perl Programming Language Design and Implementation (4th Edition)
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Perl Variables: Array Web Programming.
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.
Introduction to Perl Jarrad Battaglia.
Perl Variables: Hash Web Programming.
Data Structures – 1D Lists
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:

Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics

Basic Syntax  In Perl the first character of the variable name determines how that variable will be interpreted when the code is run.  "$" indicates a "scalar" variable  indicates an "array" variable  "%" indicates a "hash" variable  You can have three variables with the same name  For example and %x  These represent three different things

An Array Is a List of Values  For example, consider a list such as this  the number 3.14 as the first element  the string 'abA' as the second element  the number as the third element.  How do you express this list in Perl?

"Literal Representation"  Most simply  = (3.14, 'abA', 65065);  Or we can initialize from variables  my $pi = 3.14;  my $s = 'abA’;  = ($pi, $s, 65065);  We can also do integer ranges  = (-1..5); # shorthand for  Counting down not allowed! ‘abA’ ‘abA’65065

Array Variables and Assignment  my $pi = 3.14;  my $x = 65065;  = ($pi, 'abA', $x);  = (-1..5);  = ‘abA’ ‘abA’

Array Variables and Assignment  my ‘abA’ ‘abA’

 my $first = $z[0]; #  $z[0] = 2; # assign a new value to the 1 st item  $first = $z[0]; #  my $max_index = $#z; # 11  my $last = $z[$#z]; # ‘abA’ Getting at Array Elements 25 [0][1][2][3][4][5][6][7][8][9][10][11]

 Add 9 to the end (or top)  9; Push ‘abA’ beginend ‘abA’ [11] [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] [12] [0][1][2][3][4][5][6][7][8][9][10][11] ‘abA’ top bottom [11] [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0]

 Take the 9 and 5 off the end (or top)  my $end1 =  my $end2 = Pop ‘abA’ [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ‘abA’ [11] [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] [12] ‘abA’ [11] [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] POP 9 5

 Add 9 to the beginning (or bottom)  9; Unshift ‘abA’ [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ‘abA’ [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] [11] UNSHIFT 9

Shift  Take 9 and then off the beginning  my $b1 =  my $b2 = ‘abA’ 3.14 [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ‘abA’ [11] [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ‘abA’ [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] SHIFT SHIFT

 = Reverse ‘abA’ 3.14 ‘abA’ [9] [8] [7] [6] [5] [4] [3] [2] [1] [0]

Array and Scalar Context  The notion of array and scalar context is unique to Perl. Usually you can remain unaware of it, but it comes up in the reverse function. print reverse 'abc'; abc print reverse 'abc', 'def', 'ghi' ; ghidefabc print scalar reverse 'abc'; cba my $ba = reverse 'abc'; print $ba; cba

Array and Scalar Context  The notion of array and scalar context can also be used to get the size of an array. = (1,2,3,4,5,6,7); print the number of elements in the array\n"; print $#z, ' this is max offset into "\n"; 7 the number of elements in the array 6 this is max offset into

 Using a “foreach” loop foreach my $array_value { print “$array_value\n”; }  Using a “for” loop for (my $index = 0; $index < ++$index) { my $array_value = $z[$index]; print “$array_value\n”; } Iterating Through Array Contents 3.14 ‘abA’ [0][1][2][3][4][5][6][7][8][9]

 Alphabetically:  =  This does exactly the same alphabetical sort = sort {$a cmp Sorting 3.14 ‘abA’ [0][1][2][3][4][5][6][7][8][9] ‘abA’ [0][1][2][3][4][5][6][7][8][9]

 An alphabetical sort (with only numbers in the array)  = (-1, 3, -20);  =  Need a numerical sort to sort as numbers  = sort {$a Sorting 3-20 [0][1][2] -203 [0][1][2] -203 [0][1][2]

 What happens : = sort {$a  Argument "abA" isn't numeric in sort at arraySort.pl line 19. Sorting 3.14 ‘abA’ [0][1][2][3][4][5][6][7][8][9] 0‘abA’ [0][1][2][3][4][5][6][7][9][10]

Split and Join  Split using a literal my $string = "one,two,three"; = split ",", $string; print ” - from array\n"; one two three - from array  Join it up again $string = join print $string, " - rejoined with colons\n"; one two three - from array one:two:three - rejoined with colons

Split and Join  Split using a regular expression my $string = "one1two22three333fin"; = split /\d+/, $string; print "\n"; one two three fin

Swallowing Whole Files in a Single Gulp  Read the file from stdin  = <>;  Eliminate newlines from each line 

A Hash Is a Lookup Table  Hashes use a key to find an associated value. my %translate; # the percent sign denotes a hash $translate{'atg'} = 'M'; # codon is the key $translate{'taa'} = '*'; # aa is the value $translate{'ctt'} = 'K'; # lysine, oops $translate{'ctt'} = 'L'; # leucine, fixed print $translate{'atg'}; M

Initializing & Removing Key, Value Pairs  Initializing From a List  Removing key-value pairs delete $translate{'taa'}; %translate = ( 'atg' => ’M’, 'taa' => ‘*’, 'ctt' => ‘L’, 'cct' => ’P’ );

Checking if a key exists if (exists $translate{‘atg’}) { print “Methionine found in translation table\n”; } else { print “Methionine not found in translation table\n”; } if (exists $translate{‘ata’}) { print “Isoleucine found in translation table\n”; } else { print “Isoleucine not found in translation table\n”; } Methionine found in translation table Isoleucine not found in translation table

Reaching into a hash = keys %translate; print " - all keys\n"; atg ctt taa - all keys = values %translate; print " - all values\n"; M L * - all values

 First get all the keys from the hash = keys %translate;  Using a “foreach” loop foreach my $key { print “The AA code for ”, $key, “ is “, $translate{$key}, “\n”; }  Using a “for” loop for (my $index = 0; $index < ++$index) { my $key = $keys[$index]; print “The AA code for “, $key, “ is ”, $translate{$key}, “\n”; } Iterating Through Hash Contents

Problem Sets  Problem #1  Exercises 1-3, page 54, Learning Perl  Problem #2  Exercises 1, page 105, Learning Perl  How the program (call it names.pl) in exercise 1 works: $ names.pl fred flinstone $ names.pl barney rubble $ names.pl wilma flinstone

References  Perl docs online perldoc.perl.org Perl docs online  Learning Perl. Schwartz and Christiansen  Chapters 3 & 6  Programming Perl. Wall, Christiansen and Schwartz  Effective Perl Programming. Hall and Schwartz