Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
While loops.
More Perl Control Flow Software Tools. Slide 2 Control Flow l We have already seen several Perl control flow statements: n if n while n for n last l Other.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
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.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Control Structures. if-elsif-else semantically the same as C/C++ syntactically, slightly different. if ($a > 0){ print “\$a is positive\n”; } elsif ($a.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Controlling Execution Dong Shao, Nanjing Unviersity.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
5 1 Data Files CGI/Perl Programming By Diane Zak.
JavaScript, Fourth Edition
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Working with Loops, Conditional Statements, and Arrays.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
BLOCK  is enclosed by curly brackets {}.  Keep in mind that blocks are always bounded by curly brackets, even if your block has only one code line.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
References and Data Structures
String and Lists Dr. José M. Reyes Álamo.
Input from STDIN STDIN, standard input, comes from the keyboard.
EECS 183 Discussion #9: It’s time to “Git” Python! November 22nd, 2016
Lecture 7: Repeating a Known Number of Times
Chapter 5 - Control Structures: Part 2
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Scripts & Functions Scripts and functions are contained in .m-files
JavaScript: Control Statements.
Agenda Control Flow Statements Purpose test statement
MATLAB: Structures and File I/O
Conditinoal Constructs Review
Arrays, For loop While loop Do while loop
Perl Variables: Array Web Programming.
Python Primer 2: Functions and Control Flow
Chapter 8 JavaScript: Control Statements, Part 2
CIT 383: Administrative Scripting
Conditinoal Constructs Review
Control Structures: for & while Loops
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
String and Lists Dr. José M. Reyes Álamo.
Chapter 6 Control Statements: Part 2
CISC124 Labs start this week in JEFF 155. Fall 2018
CSC215 Lecture Control Flow.
CIS 136 Building Mobile Apps
Controlling Program Flow
Class code for pythonroom.com cchsp2cs
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators, ternary operator ( ? : )

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. if ($i == 3) { last; } } # prints 0 1 2 ( 3 and 4 skipped) There is also “redo”, which repeats the current iteration of the loop.

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}

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.

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.

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;

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”. For splitting at tabs: split /\t/, “your string”. (the tab character is \t) split actually uses “regular expressions”, a topic we will take up shortly.

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”.

Substring and Length 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”. The keyword “length” returns the length of a string: $len = length “giraffe”; print “length of giraffe is $len\n”; # gives 7

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 file”; 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.)

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.