Programming Perl (PowerPoint)

Shared by: dfhdhdhdhjr
Categories
Tags
-
Stats
views:
5
posted:
2/15/2012
language:
English
pages:
16
Document Sample
scope of work template
							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 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…
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");
3ex.4




          Controls:
        Ifs and Loops
3ex.5
                               Controls: if ?
        Controls allow non-sequential execution of commands, and responding to
        different conditions.
        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";
        }
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
                 Comparison operators (+debt #2)
        Comparison Numeric   String
                                      if ($age == 18){
        Equal          ==     eq
                                      ...
        Not equal      !=     ne
                                      }
        Less than      <       lt
                                      if ($name eq "Yossi")...
        Greater than   >       gt
                                      if ($name ne "Yossi")...
        Less than or
                       >=      le     if ($name lt "n")...
        equal to
        Greater than
                       <=     ge
        or equal to

        if ($age = 18)...
        Found = in conditional, should be == at ...
        if ($name == "Yossi")...
        Argument "Yossi" isn't numeric in numeric eq (==) at ...
3ex.8
                 Boolean operators
    And &&   if (($age==18) || ($name eq "Yossi")){
    Or ||        ...
    Not !    }
             if (($age==18) && ($name eq "Yossi")){
                 ...
             }
             if (!($name eq "Yossi")){
                 ...
             }
                Class exercise 3a
3ex.9


        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.
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 = <STDIN>;                      my $name="";
         }                                        while ($name ne "Yossi") {
         print "correct!\n";                          chomp($name = <STDIN>);
                                                      print "Hello $name!\n";
                                                  }
3ex.11
                                Loops: foreach
         The foreach loop passes through all the elements of an array
   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]++;
                                          }
3ex.12
                                     Loops: for
    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
                                                         my $i=0;
   for (my $i=0; $i<10; $i++) {
                                                         while ($i<10){
     print "$i\n";
                                                            print "$i\n";
   }
                                                            $i++;
                                                         }



                                  These are equivalent
3ex.13
                                     Loops: for
    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

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


         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.

						
Related docs
Other docs by dfhdhdhdhjr
PowerPoint Presentation - The Radclyffe School
Views: 10  |  Downloads: 0
Recordkeeping and Record Retention
Views: 0  |  Downloads: 0
US History Sources
Views: 7  |  Downloads: 0
MAC 346 Lecture 4
Views: 0  |  Downloads: 0
Group Work An Overview
Views: 0  |  Downloads: 0