Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.

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

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: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
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.
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control 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 Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Guide To UNIX Using Linux Third Edition
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.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
Practical Extraction & Report Language PERL Joseph Beltran.
IE 212: Computational Methods for Industrial Engineering
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
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 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Introduction to Perl Part I, II, and III By: Bridget Thomson McInnes 20 January 2004.
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.
Built-in Data Structures in Python An Introduction.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
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
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
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.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
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,
Digital Text and Data Processing Tokenisation. Today’s class □ Tokenisation and creation of frequency lists □ Keyword in context lists □ Moretti and distant.
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.
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.
– Intermediate Perl 1/6/ Intermediate Perl - POD, parameters and configuration Intermediate Perl – Session 7 · POD –
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.
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.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
The Scripting Programming Language
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,
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.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Introduction to Python
Perl Variables: Array Web Programming.
Introduction to Perl Jarrad Battaglia.
Perl I/O Learning Objectives:
Presentation transcript:

Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004

Hashes Hashes are like array, they store collections of scalars Hashes are like array, they store collections of scalars... but unlike arrays, indexing is by name... but unlike arrays, indexing is by name Two components to each hash entry: Two components to each hash entry: –Keyexample : name –Value example : phone number Hashes denoted with % Hashes denoted with % –Example : %phoneDirectory Elements are accessed using {} (like [] in arrays) Elements are accessed using {} (like [] in arrays)

Hashes continued... Adding a new key-value pair Adding a new key-value pair $phoneDirectory{“Shirly”} = $phoneDirectory{“Shirly”} = –Note the $ to specify “scalar” context! Each key can have only one value Each key can have only one value $phoneDirectory{“Shirly”} = $phoneDirectory{“Shirly”} = # overwrites previous assignment # overwrites previous assignment Multiple keys can have the same value Multiple keys can have the same value Accessing the value of a key Accessing the value of a key $phoneNumber =$phoneDirectory{“Shirly”}; $phoneNumber =$phoneDirectory{“Shirly”};

Hashes and Foreach Foreach works in hashes as well! Foreach works in hashes as well! foreach $person (keys %phoneDirectory) { print “$person: $phoneDirectory{$person}”; print “$person: $phoneDirectory{$person}”;} Never depend on the order you put key/values in the hash! Perl has its own magic to make hashes amazingly fast!! Never depend on the order you put key/values in the hash! Perl has its own magic to make hashes amazingly fast!!

Hashes and Sorting The sort function works with hashes as well The sort function works with hashes as well Sorting on the keys Sorting on the keys foreach $person (sort keys %phoneDirectory) { print “$person : $directory{$person}\n”; print “$person : $directory{$person}\n”;} –This will print the phoneDirectory hash table in alphabetical order based on the name of the person, i.e. the key.

Hash and Sorting cont... Sorting by value Sorting by value foreach $person (sort {$phoneDirectory{$a} $phoneDirectory{$b}} keys %phoneDirectory) { print “$person : $phoneDirectory{$person}\n”; print “$person : $phoneDirectory{$person}\n”;} –Prints the person and their phone number in the order of their respective phone numbers, i.e. the value.

A Quick Program using Hashes Count the number of Republicans in an array Count the number of Republicans in an array %seen = (); # initialize hash to = ( “R”, “R”, “D”, “I”, “D”, “R”, “G” ); foreach $politician { $seen{$politician}++; $seen{$politician}++;} print “Number of Republicans = $seen{'R'}\n”;

Slightly more advanced program Count the number of parties represented, and by how much! Count the number of parties represented, and by how much! %seen = (); # initialize hash to = ( “R”, “R”, “D”, “I”, “D”, “R”, “G” ); foreach $politician { $seen{$politician}++; $seen{$politician}++;} foreach $party (keys %seen) { print “Party : $party. Num reps: $seen{$party}\n”; print “Party : $party. Num reps: $seen{$party}\n”;}

Command Line Arguments Command line arguments in Perl are extremely is the array that holds all arguments passed in from the command line. – –Example:   %./prog.pl arg1 arg2 arg3 – would contain ('arg1', arg2', 'arg3) $#ARGV returns the number of command line arguments that have been passed. – –Remember $#array is the size of the array!

Quick Program Simple program called log.pl that takes in a number and prints the log base 2 of that number; #!/usr/local/bin/perl -w $log = log($ARGV[0]) / log(2); print “The log base 2 of $ARGV[0] is $log.\n”; Run the program as follows: – –% log.pl 8 This will return the following: – –The log base 2 of 8 is 3.

Another Example Program You want to print the binary form of an integer You want to print the binary form of an integer #!/usr/local/bin/perl -w foreach $integer { # converts the integer to a 32 bit binary number # converts the integer to a 32 bit # Store the last 4 elements # Store the last 4 elements into = # Print the integer and its binary form # Print the integer and its binary form print “$integer print “$integer

$_ Perl default scalar value that is used when a variable is not explicitly specified. Can be used in – –For Loops – –File Handling – –Regular Expressions

$_ and For Loops Example using $_ in a for = ( “Perl”, “C”, “Java” ); { print $_. “is a language I know\n”; } – –Output : Perl is a language I know. C is a language I know. Java is a language I know.

$_ and File Handlers Example in using $_ when reading in a file; while( <> ) { chomp $_; # remove the newline = split/ /, $_; # split the line on white space # and stores data in an array } Note: – –The line read in from the file is automatically store in the default scalar variable $_

$_ and File Handling cont.. Another example similar to the previous example: while(<>) { chomp; # removes trailing newline = split/ /; # splits the line on white # space and stores the data # in the array } Notes: – –The functions chomp and split automatically perform their respective operations on $_.

Example Program Count the number of words in a text and display the top 10 most frequency words. Count the number of words in a text and display the top 10 most frequency words.#!/usr/local/bin/perl %vocab = (); $counter = 0; while(<>) { chomp; foreach $element (split/ /) { $vocab{$element}++; } } foreach $word (sort {$vocab{$b} $vocab{$a}} %vocab) { print “$word $vocab{$word}\n”; print “$word $vocab{$word}\n”; if($counter == 10) { last; } $counter++; if($counter == 10) { last; } $counter++;}

$_ and Regular Expressions Example in using $_ when using regular expressions while( <> ) { chomp; $_=~s/[.!,;]/ /; $_=~s/I am/Why are you/; print “$_?\n”; } Input line : – –I am feeling down today. Output : – –Why are you feeling down today?

Perl Modules What are Perl Modules? What are Perl Modules? –Batches of reusable code –Allow for object oriented Perl Programming Comprehensive Perl Archive Network (CPAN) Comprehensive Perl Archive Network (CPAN) –Perl utilities –Perl Modules –Perl documentation –Perl distribution

CPAN Organization CPAN Material is organized by CPAN Material is organized by –Modules –Distributions –Documentation –Announcements –Ports –Scripts –Authors Distributions are ‘tar-gzipped’ Distributions are ‘tar-gzipped’ –tar –gzip

Categories of Modules by-author by-author –Modules are organized by author’s registered CPAN name by-category by-category –Modules categorized by subject matter by-module by-module –Modules categorized by module name

Installing a Module After you have gunziped and untared your module you have two options on installing your module depending upon if you have root privileges to the location where perl modules are installed or you don’t. After you have gunziped and untared your module you have two options on installing your module depending upon if you have root privileges to the location where perl modules are installed or you don’t. If you have root privileges or write access: If you have root privileges or write access:  perl Makefile.PL  make  make test  make install

Local Install Need to specify where you would like the module to be installed by setting the PREFIX argument when generating a Makefile from Makfile.PL Need to specify where you would like the module to be installed by setting the PREFIX argument when generating a Makefile from Makfile.PL –perl Makefile.PL PREFIX=/home/Perl/Modules –make –make test –make install

Local Install cont… Perl usually looks in system wide areas for modules, therefore it will not find your local module unless you tell Perl where to find it. Perl usually looks in system wide areas for modules, therefore it will not find your local module unless you tell Perl where to find it. In your perl program where you will be using your module In your perl program where you will be using your module#!/usr/local/bin/perl use lib ‘ ’ use ModuleName;

‘using’ a Perl Module If we have a module called TestModule that contains a function test_me(). To use this module we have two options: If we have a module called TestModule that contains a function test_me(). To use this module we have two options: –Object Oriented use TestModule; $test = TestModule->new() $test->test_me() –Standard use TestModule test_me();

Example Program #!/usr/local/bin/perl use lib ‘home/cs/bthomson/PerlModules/Suffix.pm’ use Suffix.pm $sarray->Array::Suffix->new();$sarray->create_files(“hamlet.txt”);$sarray->get_ngrams();

Module Documentation Perl Module Documentation is provided by the module author and usually written in pod format Perl Module Documentation is provided by the module author and usually written in pod format To view the documentation on the command line To view the documentation on the command line –%perldoc modulename Convert the pod document to the format of your choice: Convert the pod document to the format of your choice: –pod2textconverts to a text file –pod2htmlconverts to an html file –pod2manconverts to a man page file

Thank you Thank you