Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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)

2 3ex.2 Revision: variables & arrays Variable declaration my ($priority); Array declaration @a = ('A','B','C','D'); Array element: print $a[1]; B $a[0] = '*'; *BCD Array size: print scalar(@b); 4 Reading a list of lines: @a = ; 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 …

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

4 3ex.4 Controls: Ifs and Loops

5 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"; }

6 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"; }

7 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

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

9 3ex.9 Class exercise 3a Ask the user for his grades average and: 1.print "wow!" if it is above 90. 2.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.

10 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"; }

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

12 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

13 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 my @numArr = ; for (my $i=0; $i<scalar(@numArr); $i++) { $numArr[$i]++; } my @numArr = ; my $lastIndex = scalar(@numArr)-1; my $index; foreach $index (0..$lastIndex) { $numArr[$index]++; } These are equivalent

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

15 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

16 3ex.16 Class exercise 3b 1.Read a list of numbers (on separate lines) and print each number multiplied by 10. 2.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.


Download ppt "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."

Similar presentations


Ads by Google