Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)

Slides:



Advertisements
Similar presentations
Lecture 6 More advanced Perl…. Substitute Like s/// function in vi: #cut with EcoRI and chew back $linker = “GGCCAATTGGAAT”; $linker =~ s/CAATTG/CG/g;
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.
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.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
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.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong. Today’s Topics File input/output – open, References Perl modules Homework 2: due next Monday by midnight.
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
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:
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
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.
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 .
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.
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 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
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 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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 Programming the WWW I CMSC Winter 2003.
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)
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.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Perl Ed Finegan. Overview of Pearl Perl is a high-level programming language written by Larry Wall. It derives from the C programming language and to.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Perl Variables: Array Web Programming.
Introduction to Perl Jarrad Battaglia.
Perl Variables: Hash Web Programming.
Perl I/O Learning Objectives:
Presentation transcript:

Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)

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

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

Hashes and Foreach Foreach works in hashes as well! foreach $person (keys (%phoneDirectory) ) { 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!!

Hashes and Sorting The sort function works with hashes as well Sorting on the keys foreach $person (sort keys %phoneDirectory) { 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 foreach $person (sort {$phoneDirectory{$a} $phoneDirectory{$b}} keys %phoneDirectory) { print “$person : $phoneDirectory{$person}\n”; } Prints the person and their phone number in the order of their respective phone numbers, i.e. the value.

Exercise Chose your own test or use wget “ Identify the 100 most frequent words

Counting Words while ($l= ) (/\s+/, $l); foreach $word { $word=~s/[sx]$//;#plurial elimination $seen{$word}++; } foreach $word (sort {$seen{$a} $seen{$b}} keys %seen) { print “Word $word N: $seen{$word}\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 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.

$_ 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. #!/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”; 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? Batches of reusable code Allow for object oriented Perl Programming Comprehensive Perl Archive Network (CPAN) Perl utilities Perl Modules Perl documentation Perl distribution

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

Categories of Modules by-author Modules are organized by author’s registered CPAN name by-category Modules categorized by subject matter 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. 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 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. 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: 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 To view the documentation on the command line %perldoc modulename 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