A primer on Perl programming First structures (with examples)

Slides:



Advertisements
Similar presentations
Perl Practical Extration and Reporting Language An Introduction by Shwen Ho.
Advertisements

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.
CS 4432query processing - lecture 161 CS4432: Database Systems II Lecture #16 Join Processing Algorithms Professor Elke A. Rundensteiner.
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
● Perl reference
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.
Perl Basics Chapters 1-6 of “Learning Perl” By Randal Schwartz, Tom Christiansen & Larry Wall; ISBN , 302 pages. Second Edition, July 1997.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
Lecture 2 BNFO 240. Perl online references
8.1 Hashes (associative arrays). 8.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Dudi Enter a phone.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
3.1 Ifs and Loops. 3.2 Revision: variables Scalar variables can store scalar values: Variable declaration my ($priority); Numerical assignment $priority.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
MCB 5472 Assignment #6: HMMER and using perl to perform repetitive tasks February 26, 2014.
Print 'Hello world.'; Tren Griffith. Outline:  Perl introduction  Scalar Data  Variables  Operators  Control Structures  Input  Lists and Arrays.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
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.
Strings CS303E: Elements of Computers and Programming.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Intro to PHP Carl-Erik Svensson. What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and.
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.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Comments in PHP In PHP, we use // to make a singleline comment or /* and */ to make a large comment block. Comment is a part of your PHP code that will.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
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.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
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.
CPTG286K Programming - Perl Chapter 4: Control Structures.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CS 105 Perl: Basic I/O, Context, Strings, Lists Nathan Clement January 22, 2014.
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
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 NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
8.1 Common Errors – Exercise #3 Assuming something on the variable part of the input file. When parsing a format file (genebank, fasta or any other format),
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
More Python Proglan Python Session 2. Lists  Lists are like flexible arrays. They can contain as many variables as you wish, and all variable types are.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Interpolation Variable Interpolation, Backslash Interpolation.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
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.
Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
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
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.
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.
Introduction to Perl Jarrad Battaglia.
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
Karan Thaker CS 265 Section 001
Challenge Guide Grade Code Type Slides
Presentation transcript:

A primer on Perl programming First structures (with examples)

A primer on Perl programming Thanks for using the blog

A primer on Perl programming Thanks for using the blog

A primer on Perl programming Perl is popular among bioinformaticists because its easy, has a huge amount of modules and was born to parse text. A program takes an input and returns some elaborated output. We need to: test conditions, and iterate blocks of instructions.

A primer on Perl programming To reverse complement a multifasta file we need to: Input: filename Open the file and parse it (headers, sequences) E.g.: IF line starts with > its a header (one line), else concatenate lines… Output: Print header as is, reverse and complement the sequence

Recall: variables $scalar contains a single value (number or is an ordered list of scalars %hash is a unordered list of values with a name (key). %age = ('Andrea' => 29, 'Cesare' => 80); $age{'Carlo'} = 39; print $name is $age{$name} old.\n;

The foreach cycle Performs a block of instruction for each element of an = ('Aldo', 'John', 'Jack'); foreach $name { $count++; print Hello $name!\n; } print There were $count people in the array!\n;

The for cycle When you have to repeat a set of instruction a defined number of times. for (initial condition; until; ending instuction ) {...block... } for ($i = 1; $i<=10; $i++) { print \$i now is $i\n; }

Iterations #print numbers 1-10 in three different ways $i = 1; while ($i<=10) { print "$i\n"; $i++; } for ($i=1; $i<=10; $i++) { print "$i\n"; } foreach $i (1,2,3,4,5,6,7,8,9,10) { print "$i\n"; }

Logical operators They are different for numbers and strings!!! Equal: == or eq $a == 10 $a eq 'hello' Not equal: != or ne Greater than: >, >= Less than: <, <=

Operators Same precedence as in math +, -, *, / Modulus (division remainder): % Power: ** Concatenate strings:. $seqlength = 18; print There are.$seqlength**4. combinations\n;

Math functions Some functions: abs($x): absolute value int($x): integer part rand($x): random number up to $x sqrt($x): square root $percentage = int(100*$done/$todo); $dice = int(1+rand(6));

String functions Simple as: $length = length(Put a string here); Less simple as: $portion = substr($string, $start, = split(/;/, $string); A world apart: Pattern matching $string=~/TATA/;

If statement Executes a block of code if a condition is TRUE. You can use boolean logic (and, or). Quite simple: if (length($a) == 10) { print Yes, its ten characters long!\n; } else { print No, \$a is not as long as I want.\n; }

IF… ELSE… Only one condition: if (condition) { block… } else { block… } Multiple conditions: elsif (other condition)

Talking to scripts Parameters passed from command line are stored (uppercase!) Eg: $ perl myscript.pl 100 Genomics print First parameter: $ARGV[0]\n; print Last one: GUESS\n; Can you write a script that prints the number of parameters passed by the users plus a list of them?

Reading a file open(NAME, Filename) || die Unable to read\n; while ( ) { print $_; } Dont be scared!