4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.

Slides:



Advertisements
Similar presentations
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Advertisements

4.1 Reading and writing files. 4.2 Open a file for reading, and link it to a filehandle: open(IN, "
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
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.
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) {
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)
Perl File I/O Software Tools. Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN.
Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.
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.
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:
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.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Flow of Control Part 1: Selection
Bioinformatics 生物信息学理论和实践 唐继军
1 Module 3 Conditional Statements. Objectives  Conditional test statements to compare numerical and string data values  Looping statements to repeat.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
3.1 Revision: variables & arrays Array declaration Array = ('A','B','C','D'); = = (); Array.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
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
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Part 4 Arrays: Stacks foreach command Regular expressions: String structure analysis and substrings extractions and substitutions Command line arguments:
Perl Scripting II Conditionals, Logical operators, Loops, and File handles Suzi Lewis Genome Informatics.
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.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
The Scripting Programming Language
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Basic concepts of C++ Presented by Prof. Satyajit De
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Chapter 5 - Control Structures: Part 2
ITM 352 Flow-Control: if and switch
Arrays, For loop While loop Do while loop
Control Structures: if Conditional
Control Structures: for & while Loops
Lesson 2. Control structures File IO - reading and writing Subroutines
Logical Operations In Matlab.
Computer Science Core Concepts
2.6 The if/else Selection Structure
The Selection Structure
More Loops Topics Relational Operators Logical Operators for Loops.
Perl Control Flow Learning Objectives:
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

4.1 Controls: Ifs and Loops

4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print "Here is your beer!\n"; } print "How old are you?\n"; my $age = ; # Read number if ($age < 18) { print "How about some orange juice?\n“; } Note the indentation: a single tab in each line of new block ‘ } ’ that ends the block should be in the same indentation as where it started

4.3 Comparison operators StringNumericComparison eq== Equal ne!= Not equal lt< Less than gt> 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")... if ($age = 18)... Found = in conditional, should be == at... if ($name == "Yossi")... Argument "Yossi" isn't numeric in numeric eq (==) at...

4.4 if, else example Consider the following code: my $luckyNum = 42; print "Guess a number between 1 and 100.\n"; my $num = ; if ($num != $luckyNum) { print "Sorry, wrong guess...\n"; } else { print "WOW – you are right!!!\n"; }

4.5 if, elsif, else Sometimes it’s convenient to test several conditions in one if structure: my $luckyNum = 42; print "Guess a number between 1 and 100.\n"; my $num = ; if ($num > 100) { print "Number out of range\n"; } elsif ($num < 1) { print "Number out of range\n"; } elsif ($num != $luckyNum) { print "Sorry, wrong guess...\n"; } else { print "WOW – you are right!!!\n"; }

4.6 Boolean operators if (($age==18) and ($name eq "Yossi")){ print "Hi Yossi18"; } if (($age==18) or ($name eq "Yossi")){ print "Hi You!"; } if (!($name eq "Yossi")){ print "Hi man…" } and && - True if both conditions are true or || - True if at least one condition is true not ! - True if condition is False and && - True if both conditions are true or || - True if at least one condition is true not ! - True if condition is False

4.7 my $luckyNum = 42; print "Guess a number between 1 and 100.\n"; my $num = ; if ($num > 100) { print "Number out of range\n"; } elsif ($num < 1) { print "Number out of range\n"; } elsif ($num != $luckyNum) { print "Sorry, wrong guess...\n"; } else { print "WOW – you are right!!!\n"; } Boolean operators

4.8 my $luckyNum = 42; print "Guess a number between 1 and 100.\n"; my $num = ; if ($num > 100 or $num < 1) { print "Number out of range\n"; } elsif ($num != $luckyNum) { print "Sorry, wrong guess...\n"; } else { print "WOW – you are right!!!\n"; } Boolean operators

4.9 if (defined... ) There is an option to check whether a variable was defined: ($inFile) open(IN, "<$inFile") or die "cannot open $inFile"; = ; if (defined $arr[9]) { print "$arr[9]\n"; }

4.10 You can ask questions about a file or a directory name (not filehandle): if (-e $name) { print "The file $name exists!\n"; } -e$name exists -r$name is readable -w$name is writable by you -z$name has zero size -s$name has non-zero size (returns size) -f$name is a file -d$name is a directory -l$name is a symbolic link -T$name is a text file -B$name is a binary file (opposite of -T). File Test Operators

4.11 Class exercise 4a 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 message if the number is negative or higher than 100 (Use the or operator). 4*.Ask the user for a file name – and print a message stating whether or not the file exist.

4.12 Loops: while Commands inside a loop are executed repeatedly (iteratively). The while loop is "repetitive if": executed while the condition holds. my $luckyNum = 42; print "Guess a number\n"; my $num = ; while ($num != $luckyNum) { print "Wrong. Guess again.\n"; $num = ; } print "Correct!!\n"; my $luckyNum = 42; print "Guess a number\n"; my $num = <STDIN>; if ($num != $luckyNum) { print "wrong number..\n"; } else { print "Correct!!\n"; }

4.13 Loops: while (defined …) Let's observe the following code : open (IN, "<numbers.txt"); my $line = ; while (defined $line) { chomp $line; if ($line > 10) { print $line; } $line = ; } close (IN);

4.14 Loops: while Let's observe the following code to compute factorial. (example 5! = 1*2*3*4*5 = 120). my $num = ; my $factorial = 1; my $currentNumber = 1; while ($currentNumber <= $num) { $factorial = $factorial*currentNumber; $currentNumber++; } print "Factorial of $num is $currentNumber\n";

4.15 Loops: foreach The foreach loop passes through all the elements of an array = ("Yossi","Daiana","Jojo"); my $name; foreach $name { print "Hello $name!\n" }

4.16 Loops: foreach The foreach loop passes through all the elements of an array open (IN, "<numbers.txt"); = ; my $num; foreach $num { if ($num > 10) { print $num; } close (IN); open (IN, "<numbers.txt"); my $line = ; while (defined $line) { chomp $line; if ($line > 10) { print $line; } $line = ; } close (IN);

4.17 Breaking out of loops next – skip to the next iteration last – skip out of the loop open (IN, "<numbers.txt"); = ; foreach my $line { if ($line eq "") { last; } if ($line <= 10) { next; } print $line; } close (IN);

4.18 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

4.19 Fasta format Fasta format sequence begins with a single-line description, that start with ' > ', followed by lines of sequence data that contains new-lines after a fixed number of characters: >gi| |ref|NP_ | thr operon leader peptide… MKRISTTITTTITITTGNGAG >gi| |ref|NP_ | fused aspartokinase I and homoserine… MG1655]MRVLKFGGTSVANAERFLRVADILESNARQGQVATVLSAPAKITNHLVAMIEKTISGQDALPN AKFFAALARANINIVAIAQGSSERSISVVVNNDDATTGVRVTHQMLFNTDQVIEVFVIGVGGVGGALLEQ NAGDELMKFSGILSGSLSYIFGKLDEGMSFSEATTLAREMGYTEPDPRDDLSGMDVARKLLILARETGRE LELADIEIEPVLPAEFNAEGDVAAFMANLSQLDDLFAARVAKARDEGKVLRYVGNIDEDGVCRVKIAEVD GNDPLFKVKNGENALAFYSHYYQPLPLVLRGYGAGNDVTAAGVFADLLRTLSWKLGV >gi| |ref|NP_ | homoserine kinase [Escherichia coli… MG1655]MVKVYAPASSANMSVGFDVLGAAVTPVDGALLGDVVTVEAAETFSLNNLGRFADKLPSEPREN IVYQCWERFCQELGKQIPVAMTLEKNMPIGSGLGSSACSVVAALMAMNEHCGKPLNDTRLLALMGELEGR ISGSIHYDNVAPCFLGGMQLMIEENDIISQQVPGFDEWLWVLAYPGIKVSTAEARAILPAQYRRQDCIAH GRHLAGFIHACYSRQPELAAKLMKDVIAEPYRERLLPGFRQARQAVAEIGAVASGISGSGPTLFALCDKP ETAQRVADWLGKNYLQNQEGFVHICRLDTAGARVLEN

4.20 Class exercise 4b 1.Read a file containing several proteins sequences in FASTA format, and print only their header lines using a while loop (see example FASTA file on the course webpage). 2.Read a file containing several proteins sequences in FASTA format, and print only their header lines using a foreach loop (see example FASTA file on the course webpage). 3.(Ex 3.1b) Read a file containing numbers, one in each line and print the sum of these numbers. (use number.txt from the website as an example). 4*.Read the "fight club.txt" file and print the 1 st word of the 1 st line, the 2 nd word of the 2 nd line, and so on, until the last line. (If the i-th line does not have i words, print nothing).