1 / 16

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). Note: use strict on the first line. Revision: variables & arrays. Variable declaration my ($priority); Array declaration @a = ('A','B','C','D');

ona
Download Presentation

Note: use strict on the first line

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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) Note: use strict on the first line

  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 = <STDIN>; 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. 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. Controls:Ifs and Loops

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

  6. 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";} if, elsif, else

  7. Comparison operators (+debt #2) 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 ...

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

  9. Class exercise 3a Ask the user for his grades average and: • print "wow!" if it is above 90. • print "wow!" if it is above 90; "well done." if it is above 80 and "oh well" if it is lower. • 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. 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 = <STDIN>;}print "correct!\n"; Loops: while my $name="";while ($name ne "Yossi") { chomp($name = <STDIN>); print "Hello $name!\n";}

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

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

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

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

  15. Breaking out of loops die– end the program and print an error message to the standard error <STDERR> 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. Class exercise 3b • Read a list of numbers (on separate lines) and print each number multiplied by 10. • Read several protein sequences in FASTA format, and print only their header lines. (see example FASTA file on the course webpage). • 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.

More Related