I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){

Slides:



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

● Perl reference
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
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.
Introduction to Algorithms Chapter 7: Quick Sort.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
COS 381 Day 19. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
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.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
File/Directory manipulation. Opening a File To read from a file, must use a file handle. –by convention, all caps open a file (for reading): –open FILE,
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
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.
Perl Refernces. Kinds of references: hard: a scalar variable that points to data symbolic: a variable that names another reference typeglob: a kind of.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Introduction to Perl Yupu Liang cbio at MSKCC
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.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
Introduction to Unix – CS 21
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl II Part III: Motifs and Loops. Objectives Search for motifs in DNA or Proteins Interact with users at the keyboard Write data to files Use loops.
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 Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
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,
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
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.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
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.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Perl Subroutines User Input Perl on linux Forks and Pipes.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Input from STDIN STDIN, standard input, comes from the keyboard.
Perl Variables: Array Web Programming.
Subroutines Web Programming.
Basic Input/Output Web Programming.
Lesson 2. Control structures File IO - reading and writing Subroutines
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
CSC 352– Unix Programming, Fall 2012
Presentation transcript:

I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){ exit(1); }

I/O (2) array of program arguments foreach $file ){ open FILE, $file; while ( ){ print $_; } foreach $file ){ open FILE, $file; print ; }

I/O (3) # does the same as the previous example # (similar to cat Unix program) while (<>){ print $_; } #even more simple print <>; #sorts print sort <>;

Filehandles open FILE, “tmp.fasta”; #open to read or open FILE, “<tmp.fasta”; #open to read open FILE, “>tmp.fasta”; #open to write open FILE, “>>tmp.fasta”; #append print FILE, “new line\n”;

Filehandles (2) open FILE, “readme.txt” or die “Cannot open file: $!”; unless( -e $filename){ print “File $filename doesn’t exist\n”; }

File tests

Filehandles; Globing Unix Prompt: ls glob “ *.txt glob “.* *.txt ”; foreach ( glob(“*.fasta”) ){ print “File name: $_\n”; }

Directory Handles opendir DIR, $dirname or die “Cannot open dir $dirname\n”; foreach $file (readdir DIR){ print $file.”\n”; } closedir DIR; chdir “/home/usr/” or die “Couldn’t change directory: = readdir DIR; $” = “\n”; print

Subroutines Subroutine definition is global. Global variables can be used within the subroutine (not a good style). Paramters are stored in the default $_[0], $_[1], … The last evaluated expression is the return value. (return will also work)

Subroutines (2) $k= &max (9,20); sub max { if( $_[0] > $_[1]){ $_[0]; #or “return $_[0];” }else{ $_[1]; # or “return $_[1];” } $k= &max (9,20,30); $k= &max (9); # $_[1] becomes undef

Subroutines (3) sub max { ($a, #might change global variables if( $a > $b){ $a; }else{ $b; } my($a, #private variables

Subroutines (4) $max_value = &max ($i,$j,$k,$l); sub max { my($max_so_far)= ; foreach ){ if($_ > $max_so_far){ $max_so_far = $_ ; } return $max_so_far; } my($max_so_far)=

use strict use strict; #from now on use strict in all assignments $i=1; #compiler will report error my $i=1; #ok sub f { $n=3; #compiler will report error my $n=3; #ok }

my $i=1; &inc( $i ); sub inc{ $i++; } #value of $i is not changed my $i=1; &inc( \$i ); sub inc{ $$i++; } Passing Values by Reference

References References: $rscalar = \$scalar; $rarray = $rhash = \%hash; $rfile = \*FILE; $rcode = \&qsort; Dereferences: print $$rscalar; #or print ${$rscalar}; #or %$rhash{“key”}= “value”; print $rfile “test \n”; &$qsort( ); $$array[3]; #OR $array->[3];

Reference to (“one”, “two”, “three”); $aref= print $$aref[0]; foreach ){ print; }

Reference to Array (“one”, “two”, “three”); #array size ($r) = (“one”, “two”, “three”); #first list element -> one $r= (“one”, “two”, “three”); #last computed value->three $r = $r= [ “one”, “two”, “three” ]; #array creation, pointer to anonymous array print $$r[0]; $r= [ “one”, “two”, [ 1, 2, 3] ];

Reference to File open LOG, “>log.txt”; writelog( \*LOG, “process is running”); sub writelog{ my $LOG = shift; print $LOG scalar( localtime(time) ), }

Reference to Function open LOG, “>log.txt”; $r= \&writelog; &$r(\*LOG, “process is running”); sub writelog{ my $LOG = shift; print $LOG scalar( localtime(time) ), }

Ref $rarray= $rrarray = \$rarray; print ref $rarray, " ", ref $rrarray, "\n"; Output: ARRAY REF REF ARRAY SCALAR HASH CODE (function) GLOB (file)

HomeWork Implement QuickSort (divide-and-conquer) Divide: The array A[p..r] is partitioned into two nonempty subarrays A[p..q] and A[q+1..r] such that each element of A[p..q] is less than or equal to each element of A[q+1..r]. The index q is computed as part of this procedure. Conquer: The two subarrays A[p..q] and A[q+1..r] are sorted by recursive calls to quicksort. Input: Strings with numbers separated by whitespaces. Output: Single string of sorted numbers separated by space.