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.

Slides:



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

CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
4ex.1 More loops. 4ex.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) {
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
5.1 Previously on... PERL course (let ’ s practice some more loops)
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
4.1 Revision. 4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
3.1 Ifs and Loops. 3.2 Revision: variables Scalar variables can store scalar values: Variable declaration my ($priority); Numerical assignment $priority.
4.1 More loops. 4.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) { $num.
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
If statements while loop for loop
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics 生物信息学理论和实践 唐继军
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
1 Module 3 Conditional Statements. Objectives  Conditional test statements to compare numerical and string data values  Looping statements to repeat.
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 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.
Chapter 4: Control Structures II
3.1 Revision: variables & arrays Array declaration Array = ('A','B','C','D'); = = (); Array.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (2) Ruibin Bai (Room AB326) Division of Computer Science The University.
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
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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,
Getting started in Perl: Intro to Perl for programmers Matthew Heusser – xndev.com - Presented to the West Michigan Perl User’s Group.
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.
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)
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
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),
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
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.
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
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
2.1 Lesson 2: Scalar Functions and Arrays “Perl programming is an empirical science!” - Larry Wall.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
CSI605 perl. Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
The Scripting Programming Language
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.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Chapter 5 - Control Structures: Part 2
3rd prep. – 2nd Term MOE Book Questions.
More Loops.
Control Structures: for & while Loops
2.6 The if/else Selection Structure
The Selection Structure
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

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 (so remove the #!... line)

3ex.2 Revision: variables & arrays Variable declaration my ($priority); Array = ('A','B','C','D'); Array element: print $a[1]; B $a[0] = '*'; *BCD Array size: print 4 Reading a list of = ; Reminder: Each line is a different array element Hit Ctrl-z to end input No Ctrl-z in Perl Express – you'll have to use the Command Prompt …

3ex.3 Debt #1: arrays $str = = split("-", $str); $str = join("!! ); print "$str\n"; So!! Long!! and!! thanks!! for!! all!! the!! !! fish reverse(5,4,3,2,1);sort("Ohel","Bait","Gamal");

3ex.4 Controls: Ifs and Loops

3ex.5 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions. else { print "Are you doing anything tomorrow night?\n"; } print "How old are you?\n"; my $age = ; if ($age < 18) { print "Sorry, I’m not allowed to chat with minors\n"; }

3ex.6 if, elsif, else It’s convenient to test several conditions in one if structure: if ($age < 18) { print "Sorry, I’m not allowed to chat with minors"; print "\n"; } elsif ($age < 25) { print "Are you doing anything tomorrow night?\n"; } elsif ($age < 35) { print "Are you married?\n"; } else { print "Do you need help crossing the street?\n"; }

3ex.7 if ($age = 18)... Found = in conditional, should be == at... if ($name == "Yossi")... Argument "Yossi" isn't numeric in numeric eq (==) at... Comparison operators (+debt #2) StringNumericComparison eq==Equal ne!=Not equal lt<Less than gtgt>Greater than le>= Less than or equal to ge<= Greater than or equal to if ($age == 18){... } if ($name eq "Yossi")... if ($name ne "Yossi")... if ($name lt "n")... NumericComparison ==Equal !=Not equal <Less than >Greater than >= Less than or equal to <= Greater than or equal to

3ex.8 Boolean operators if (($age==18) || ($name eq "Yossi")){... } if (($age==18) && ($name eq "Yossi")){... } if (!($name eq "Yossi")){... } And && Or || Not !

3ex.9 Class exercise 3a Ask the user for his grades average and: 1.print "wow!" if it is above print "wow!" if it is above 90; "well done." if it is above 80 and "oh well" if it is lower. 3.print "wow!" if it is above 90; "well done." if it is above 80 and "oh well" if it is lower. Print an error massage if the number is negative or higher than 100 (Use the or operator). 4*.print "boom" if the number is divisible by 7. (The % operator gives the remainder. [$x % $y gives the remainder of $x divided by $y]) 5*.print "mega boom" if the number is divisible by 7 and by 2, or it equals 99.

3ex.10 Loops: while Commands inside a loop are executed repeatedly (iteratively): The while loop is "repetitive if": executed while the condition holds. my $num=0; print "Guess a number.\n"; while ($num != 31) { $num = ; } print "correct!\n"; my $name=""; while ($name ne "Yossi") { chomp($name = ); print "Hello $name!\n"; }

3ex.11 Loops: foreach The foreach loop passes through all the elements of an array = ; my $name; foreach $name { print "Hello $name!\n"; } = ; my $lastIndex = my $index; foreach $index (0..$lastIndex) { $numArr[$index]++; }

3ex.12 Loops: for The for loop is controlled by three statements: 1 st is executed before the first iteration 2 nd is the stop condition 3 rd is executed before every re-iteration for (my $i=0; $i<10; $i++) { print "$i\n"; } my $i=0; while ($i<10){ print "$i\n"; $i++; } These are equivalent

3ex.13 Loops: for The for loop is controlled by three statements: 1 st is executed before the first iteration 2 nd is the stop condition 3 rd is executed before every re-iteration = ; for (my $i=0; $i++) { $numArr[$i]++; } = ; my $lastIndex = my $index; foreach $index (0..$lastIndex) { $numArr[$index]++; } These are equivalent

3ex.14 Breaking out of loops next – skip to the next iteration last – skip out of the loop = ; foreach $line { if (substr($line,0,1) eq ">") { next; } if (substr($line,0,8) eq "**stop**") { last; } print $line; }

3ex.15 Breaking out of loops die – end the program and print an error message to the standard error if ($score < 0) { die "score must be positive"; } score must be positive at test.pl line 8. Note: if you end the string with a "\n" then only your message will be printed * warn does the same thing as die without ending the program

3ex.16 Class exercise 3b 1.Read a list of numbers (on separate lines) and print each number multiplied by Read several protein sequences in FASTA format, and print only their header lines. (see example FASTA file on the course webpage). 3.Read a line containing numbers separated by spaces and print the sum of these numbers. 4*.Read list of names (first and last name), one in each line, until "END" is entered. Print out a list of sorted last names.