Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3.

Similar presentations


Presentation on theme: "More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3."— Presentation transcript:

1 More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3. To understand the limitation of using some control flow statements

2 COMP111 Lecture 12 / Slide 2 Perl Arrays and Lists Table of Content  Control Flow  Unless  Until  do … while  do … until  foreach  foreach $_  “last” again  Next  Redo  Next & redo  Redo & last/next  Labeled Blocks  Labeled Blocks Examples  Backward if  Backward unless, while, until  && if  || unless

3 COMP111 Lecture 12 / Slide 3 Control Flow  We have already seen several Perl control flow statements:  if  while  for  last  Other control flow statements:  unless  until  do while  do until  foreach  next  redo

4 COMP111 Lecture 12 / Slide 4 “ unless “ statement (1)  The Perl unless statement is like an if statement with the condition negated: if($temperature <= 20){ print "too cold!\n"; } unless($temperature > 20){# same thing print "too cold!\n"; } ------------------------------------------- if(!$hot){ print "too cold!\n"; } unless($hot){# same thing print "too cold!\n"; }

5 COMP111 Lecture 12 / Slide 5  unless can have else, just like if : #!/usr/local/bin/perl5 -w print "Enter temperature: "; chomp($temperature = ); unless($temperature > 20){ print "too cold!\n"; }else{ print "too hot!\n"; } “ unless “ statement (2)

6 COMP111 Lecture 12 / Slide 6  unless can also have elsif, just like if : #!/usr/local/bin/perl5 -w print "Enter temperature: "; chomp($temperature = ); unless($temperature >= 20){ print "too cold!\n"; }elsif($temperature == 20){ print "ok!\n"; }else{ print "too hot!\n"; } “ unless “ statement (3)

7 COMP111 Lecture 12 / Slide 7  The Perl until statement is like an while statement with the condition negated.  Sometimes is is easier to say “ until something is true ” rather than “ while not this is true ” : while(!$endoffile){... } until($endoffile){# same thing... } “ until “ statement (1)

8 COMP111 Lecture 12 / Slide 8  The until statement loops indefinitely, until the condition is true, such as a user-controlled condition: #!/usr/local/bin/perl5 -w $resp = "no"; until($resp eq "yes"){ print "Wakeup [yes/no]? "; chomp($resp = ); } $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $ “ until “ statement (2)

9 COMP111 Lecture 12 / Slide 9  The do while statement is like the C++ do while statement.  It loops indefinitely, while the condition is true, such as a user- controlled condition:  do while always executes the body of the loop at least once. #!/usr/local/bin/perl5 -w do{ print "Wakeup [yes/no]? "; chomp($resp = ); }while($resp ne "yes"); $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? yes $ “ do … while “ statement (1)

10 COMP111 Lecture 12 / Slide 10  The do until statement loops indefinitely, until the condition is true, such as a user-controlled condition.  do until always executes the body of the loop at least once. #!/usr/local/bin/perl5 -w do{ print "Wakeup [yes/no]? "; chomp($resp = ); }until($resp eq "yes"); $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $ “ do … until “ statement (1)

11 COMP111 Lecture 12 / Slide 11  foreach takes a list of values and assigns them one by one to a scalar variable.  The body of the loops is executed once for each successive assignment.  foreach is similar to the shell programming ’ s for statement. foreach $i (@some_list){... } “ foreach “ statement (1)

12 COMP111 Lecture 12 / Slide 12  The following example sums the contents of an array: $ cat sum #!/usr/local/bin/perl5 -w @a = (21,32,3,44,75,16,19); $sum = 0; foreach $b (@a){ $sum += $b; } print "The array sum is: $sum\n"; $ sum The array sum is: 210 $ “ foreach “ statement (2)

13 COMP111 Lecture 12 / Slide 13  foreach allows us to easily print an array in our own customized way.  The following example prints an array with each element separated by 2 spaces: $ cat print1 #!/usr/local/bin/perl5 -w @a = (1,2,3,4,5); foreach $i (@a){ print "$i "; } print "\n"; $ print1 1 2 3 4 5 $ “ foreach “ statement (3)

14 COMP111 Lecture 12 / Slide 14  The following example prints the numbers in reverse order without changing the array: $ cat print2 #!/usr/local/bin/perl5 -w @a = (1,2,3,4,5); foreach $i (reverse @a){ print "$i "; } print "\n"; $ print2 5 4 3 2 1 $  reverse @a is the same as writing reverse(@a). Parenthesis are always optional on Perl functions. “ foreach “ statement (4)

15 COMP111 Lecture 12 / Slide 15  If you omit the scalar variable in foreach, Perl will use $_ automatically: $ cat print3 #!/usr/local/bin/perl5 -w @a = (1,2,3,4,5); foreach (reverse @a){ print; } print "\n"; $ print3 54321 $  print (and other Perl functions) use $_ as the default if nothing is specified. “ foreach “ statement (5)

16 COMP111 Lecture 12 / Slide 16  Of course, if you want double spaces, you will have to use $_ explicitly: $ cat print3a #!/usr/local/bin/perl5 -w @a = (1,2,3,4,5); foreach (reverse @a){ print "$_ "; } print "\n"; $ print3a 5 4 3 2 1 $ “ foreach “ statement (6)

17 COMP111 Lecture 12 / Slide 17  The scalar variable in foreach is an alias for each variable in the list, not a copy. (Tricky!)  If you modify the scalar variable in foreach, the aliased element in the list is also changed: $ cat double #!/usr/local/bin/perl5 -w @a = (1,2,3,4); foreach $b (@a){ $b *= 2; } print "@a\n"; $ double 2 4 6 8 $ “ foreach “ statement (7)

18 COMP111 Lecture 12 / Slide 18  The last command only breaks out of while, until, for, and foreach loops.  For mysterious reasons, do while and do until do not count as loops in Perl. $ cat last1 #!/usr/local/bin/perl5 -w do{ print "Wakeup [yes/no]? "; chomp($resp = ); if($resp eq "yes"){ last; } }while(1); $ last1 Wakeup [yes/no]? yes Can't "last" outside a block at last1 line 6, chunk 1. $ “ last “ statement again (1)

19 COMP111 Lecture 12 / Slide 19  In the example below, the last statement causes the outer while loop to be broken: $ cat last2 #!/usr/local/bin/perl5 -w while(1){ do{ print "Wakeup [yes/no]? "; chomp($resp = ); if($resp eq "yes"){ last; } }while(1); } $ last2 Wakeup [yes/no]? yes $ “ last “ statement again (2)

20 COMP111 Lecture 12 / Slide 20  The next command causes a jump to the end of the loop, but without terminating the loop. $ cat next1 #!/usr/local/bin/perl5 -w $resp = "no"; until($resp eq "quit" || $resp eq "yes"){ print "Wakeup [yes/no/quit]? "; chomp($resp = ); if($resp eq "yes"){ next; } print "sleeping...\n"; } $ next1 Wakeup [yes/no/quit]? no sleeping... Wakeup [yes/no/quit]? yes $ next1 Wakeup [yes/no/quit]? quit sleeping... $ “ next “ statement

21 COMP111 Lecture 12 / Slide 21  The redo command causes a jump back to the beginning of the loop, but without re-testing the condition ( next re-tests the condition). $ cat redo #!/usr/local/bin/perl5 -w $resp = "no"; until($resp eq "quit" || $resp eq "yes"){ print "Wakeup [yes/no/quit]? "; chomp($resp = ); if($resp eq "yes"){ redo; } print "sleeping...\n"; } $ redo Wakeup [yes/no/quit]? no sleeping... Wakeup [yes/no/quit]? yes Wakeup [yes/no/quit]? quit sleeping... $ “ redo “ statement

22 COMP111 Lecture 12 / Slide 22  Like last, next and redo only works for while, until, for, and foreach loops (not do while and do until loops). $ cat redo1 #!/usr/local/bin/perl5 -w $resp = "no"; do{ print "Wakeup [yes/no/quit]? "; chomp($resp = ); if($resp eq "yes"){ redo; } print "sleeping...\n"; }until($resp eq "quit" || $resp eq "yes"); $ redo1 Wakeup [yes/no/quit]? no sleeping... Wakeup [yes/no/quit]? yes Can't "redo" outside a block at redo1 line 7, chunk 2. $ “next”, “redo” & looping in Perl

23 COMP111 Lecture 12 / Slide 23 redo and last/next  You can make a while loop using redo and last/next in a “ naked ” block (a “ naked ” block is a block { } that is not part of a loop or if statement): $ cat redo2 #!/usr/local/bin/perl5 -w { print "Wakeup [yes/no]? "; chomp($resp = ); if($resp eq "yes"){ last;# could also use "next;" } redo; } $ redo2 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $

24 COMP111 Lecture 12 / Slide 24 Labeled Blocks  What if you want to jump out of two nested loops?  In Perl, you can use labeled loops to break out of an outer loop using last, next, and redo.  For clarity (and other reasons), it is best to choose label names that are all upper case letters and digits.  Labels always end in a colon “ : ”. OUTERLOOP: for($i=1; $i<3; $i++){ INNERLOOP: for($j=1; $j<5; $j++){...  Add the label (without the colon) as a parameter to last, for example, if you want to exit OUTERLOOP. last OUTERLOOP;

25 COMP111 Lecture 12 / Slide 25 Labeled Blocks Example $ cat label #!/usr/local/bin/perl5 -w $sum = 0; LOOP1: for($i1=1; $i1<=2; $i1++){ LOOP2: for($i2=1; $i2<=2; $i2++){ LOOP3: for($i3=1; $i3<=2; $i3++){ $sum += 1; print "sum so far: $sum\n"; if($sum > 2){ last LOOP2; } $sum += 2; } $sum += 3; } print "sum at end: $sum\n"; $ label sum so far: 1 sum so far: 2 sum so far: 5 sum so far: 9 sum at end: 12 $

26 COMP111 Lecture 12 / Slide 26 Backward “ if ” (1)  A simple way to write “ if this, then that ” is: chomp($user = `whoami`); print("Hi Bill!\n") if($user eq "gates"); is the same as: chomp($user = `whoami`); if($user eq "gates"){ print "Hi Bill!\n"; }  Backward if avoids having to write the curly braces { }.  There can only be one statement inside the block.

27 COMP111 Lecture 12 / Slide 27  Backward if is a natural and tidy way to exit from a loop: $ cat backif #!/usr/local/bin/perl5 -w while(1){ print "Wakeup [yes/no]? "; chomp($resp = ); last if $resp eq "yes"; }; $ backif Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $ Backward “ if ” (2)

28 COMP111 Lecture 12 / Slide 28  You can also use backward unless, while, and until (if there is only one statement in the block): $ cat sum #!/usr/local/bin/perl5 -w print "Enter numbers to sum (0 to quit): \n"; $sum = 0; $n = 1; $sum += $n = while($n != 0); print "The sum is: $sum: \n"; $ sum Enter numbers to sum (0 to quit): 1 0 The sum is: 2 $ Backward “unless”, “while” & “until”

29 COMP111 Lecture 12 / Slide 29  Another simple way to write “ if this, then that ” is: chomp($user = `whoami`); $user eq "gates" && print("Hi Bill!\n"); is the same as: chomp($user = `whoami`); if($user eq "gates"){ print("Hi Bill!\n"); } && “ if ” (1)

30 COMP111 Lecture 12 / Slide 30 this && that;  Why does this work?  Isn ’ t && the logical-and operator?  Consider what happens when this and that take on values of true and false:  If this is true, then the value of the entire expression is still not known, because it depends on the value of that. So that has to be evaluated.  If this is false, there is no need to look at that, because the value of the whole expression must be false. Since there is no need to evaluate that, Perl skips it. && “ if ” (2)

31 COMP111 Lecture 12 / Slide 31  && if is also a tidy way to exit from a loop: $ cat backif1 #!/usr/local/bin/perl5 -w while(1){ print "Wakeup [yes/no]? "; chomp($resp = ); $resp eq "yes" && last; }; $ backif1 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $ && “ if ” (3)

32 COMP111 Lecture 12 / Slide 32  Similarly, another simple way to write an unless statement is: $temperature > 20 || print "too cold!\n"; is the same as: unless($temperature > 20){# same thing print "too cold!\n"; } || “ unless ”


Download ppt "More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3."

Similar presentations


Ads by Google