miscellaneous items n.
Skip this Video
Loading SlideShow in 5 Seconds..
Miscellaneous Items PowerPoint Presentation
Download Presentation
Miscellaneous Items

Miscellaneous Items

106 Vues Download Presentation
Télécharger la présentation

Miscellaneous Items

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Miscellaneous Items

  2. Loop Control • It is quite possible to get out of a loop from the middle of it, similar to the “break” and “continue” commands in C. • In Perl, “next” ends the current iteration of the loop and starts the next one: for (my $i = 0; $i < 5; $i++) { if ($i == 3) { next; } print “$i “; } # prints 0 1 2 4 ( 3 is skipped) • “last” ends the loop completely and moves the program to the lines following it. for (my $i = 0; $i < 5; $i++) { if ($i == 3) { last; } print “$i “; } # prints 0 1 2 ( 3 and 4 skipped) • There is also “redo”, which repeats the current iteration of the loop.

  3. Naked Blocks • A “block” of code is a group of statements found within curly braces : { ... }. Most blocks are part of other constructions, such as for and while loops. However, a block can be used all by itself, as a “naked” block: { do something; } • Naked blocks are executed a single time. • One use for naked blocks is to contain local variables. Remember that declaring a variable with “my” causes it to exist only within the innermost set of curly braces (block) that contains it. • Another use is temporary function definitions used from within other functions. An example is sort {$a <=> $b}

  4. Labeled Blocks • Any block can be labeled by putting a name (usually all capital letters) followed by a colon ( : ) followed by the loop: LINE: for (my $i = 0; $i <=10; $i++) { for (my $j = 0; $j <= 10; $j++) { if ($i * $j > 50) { last LINE; } } } • Block labels can be used with last, next, or redo: “last LINE” breaks control out of the outer LINE loop in the above example. • This is an easy way to break all the way out of nested loops. • It is also useful for re-doing a section of code in a naked block under some conditions but not others.

  5. Unless and Until • Perl uses “unless” to mean “if not”. Thus, these two statements are equivalent: if ($var != 5 { print; } unless ($var == 5) { print; } • Similarly, “until” means the same as “while not”. These two statements are equivalent: while ($var != 5) {print; } until ($var == 5) { print; } • I find both of these rather difficult to comprehend. Occasionally useful, but only if they make sense to you.

  6. Expression Modifiers • also known as backwards syntax (at least to me). • If you have just one statement in an “if” block: if ($var > 0) { print “$var\n”; } you can save a lot of typing by writing it: print “$var\n” if $var > 0; • Note that with this syntax you don’t need parentheses around the conditional clause. • This works for “while”, “unless”, ‘until”, and “foreach” too. • With “foreach”, you can only use $_ as the scalar: print $_, “\n” foreach @line;

  7. Split • “split” takes a string and separates it into an array of strings at whatever pattern of characters is indicated as the first argument between slashes: split /,/, “cat,dog,bird”; This expression splits the string at each comma, returning “cat”, “dog”, “bird”. The splitting characters (the comma in this case) are discarded. • Note the comma after the splitting pattern: /,/, . It is necessary! • To split a string into individual characters, use: split //, “The dog”; which gives “T”, “h”, “e”, “ “, “d”, “o”, “g”. • The split pattern can be multiple characters long. • To split at any whitespace (one or more tabs or spaces or newlines), use split /\s+/, “your string”. • split actually uses “regular expressions”, a topic we will take up shortly.

  8. Join • “join” is the opposite of split: join takes the elements of an array and joins them into a single string, separated by whatever symbol(s) you like. join “:“, “dog”, “cat”, “bird”; gives “dog:cat:bird”. • Note the comma after the joining symbol “:” ! • As with split, any number of characters can be used as the joining pattern: @ arr = (“cat”, “dog”); join “ mary had a little lamb “, @arr; gives “cat mary had a little lamb dog”.

  9. Substring • To get part of a string, based on the start position and length of the desired substring, use the “substr” operator. • Basic syntax: $my_substring = substr $string, start_pos, length • Note that the first position is 0. Thus, substr $my_string, 0, 5 takes the first 5 characters of $my_string. • If you want to get all of the string after a starting position, leave off the second number. Thus, substr $mystring, 10 returns all characters after position 10. • As with array indexes, negative numbers count in from the end of the string. Thus, substr, “The porcupine”, -5, 2 returns “up”.

  10. Logical Operators • “&&” and “and” both do the same thing, but && has a high precedence while “and” has a low precedence. • Similarly, “||” and “or” both do the same thing with || high precedence and “or” low. • These operators are often used as “short circuit” operators, joining two statements together and evaluating the second statement only if necessary. A common construction: open INFILE, “filename” or die “Couldn’t open”; The first statement, open INFILE, “filename” is evaluated. If it evaluates to “true” (i.e. if the file is properly opened), then the entire line is going to be true, because “or” statements are true even if only 1 clause is true. Thus the second statement, “or die ....”, does not need to be evaluated. It is only evaluated if the file does not open properly and the “open ...” returns “false”. • In this example, “or” is used instead of “||” to avoid precedence problems. You want the “or” part to be the last thing operated on, not the first. To use “||” it would be necessary to enclose both statements in parentheses: (open INFILE, “filename”) || (die “Couldn’t open”); • “die” causes the program to terminate, printing out whatever message follows the die. It is a useful tool for debugging. • Another example: ($var1 < $var2) && ($var1 = $var2); If the first clause is true, then the second is evaluated, and the value of $var2 is assigned to $var1. If the first clause is false, then the second clause is never evaluated, and $var 1 stays untouched. (This could be used in a program to find the minimum, for example.)

  11. Ternary Operator • Perl uses the same ternary operator as in C: “? :” It means “if true do this, if false do that”. • For example: ($var == 5) ? (print “= 5”) : (print “!= 5”); • The first clause is evaluated as either true or false. • The second clause, following the “?” is used if the first clause is true, and the third clause, following the “:”, is used if the first clause is false. • This is another case where enclosing the clauses in parentheses avoids precedence problems.