The control structure in Perl learning notes _perl

Source: Internet
Author: User
Tags goto

Type of statement

A computer is a tool that has the advantage of performing repetitive tasks. Loop execution, loop statement.
Need to make a lot of judgments, conditionally executed, conditional statements.
Sequential statements.
Summary of True and false values of expressions
An expression is not necessarily a logical expression, but it must have a true or false value.
False value:
Logical value is False
Value is 0
String is empty
List is empty
Undef
Other conditions are truth values

Second, conditional statement if

Form

Copy Code code as follows:

if (expression) {whitespace ignored, break can be different
if (expression)
Statement} {
elsif (expression) {statement
}
Statement
....
else{
Statement
}

Description

(1) elsif and else parts can be omitted.
(2) Note the Elsif keyword.
(3) statement block {} must have, but can be null
(4) can be nested.
(5) Meaning: execution when true, and expression as execution condition.

Third, conditional statement unless

Copy Code code as follows:

Unless (expression) {statement}

In contrast to the if condition meaning, the execution statement exits if the expression is true, and the expression is an exit condition.

Four, loop statement while

Copy Code code as follows:

while (expression) {statement}

(1) The expression is true when executed, is the condition of execution.
(2) must set the condition has false possibility, otherwise cannot exit, unrestricted circulation.
do{statement}while (expression)
Execute at least once, and then perform a second judgment first.

Five, circular statement until

Copy Code code as follows:

Until (expression) {statement}

(1) On the contrary, execute the statement until the expression is really stopped.
(2) stops when True, and the expression is an exit condition.
Copy Code code as follows:

do{Statement}until expression)

Execute the statement at least once, then make the conditional judgment.

Six, for Loop

for (expression 1; expression 2; expression 3) {statement}
(1) Expression 1: Initializes a control variable or other variable that executes only on the first loop and can be performed by the
Commas are separated to assign values to multiple variables.
(2) Expression 2: The loop is true when compared at each loop.
(3) Expression 3: After each execution of the loop before executing, change the value of the control variable, generally for the self increase.
(4) Three expressions can be omitted, but two must be retained. for (;;)
(5) First, three expressions can be used to execute multiple statements.

For example: for ($line =<>, $count =1; $count <10; $line =<>, $count + +) {print $line;}

Seven, the foreach statement

foreach $w (list or array) {statement}
(1) () within an array of @a, or list (1,2,3)
(2) The $w can be a new variable or a variable that has been used, and the value of the variable is restored after the end of the loop.
(3) Each loop assigns the values in the list or array to $w until the list is empty, so you don't have to care about the length of the array.
(4) If the value of the $w is modified in the loop body, the corresponding data element value is also changed.
(5) When the list is a constant, the value of the $w cannot be modified.
(6) $w and @a can be the same, such as $a (@a)
(7) for and foreach can be interchangeable, generic.

The form of a foreach
(1) foreach $a (@a) {} general form.
(2) foreach $a (1,2,3,4) {} list
(3) foreach $k (keys%h) {} hash form
(4) foreach $a (@a[1,2,3]) {} Only some elements of an array
(5) foreach (@a) {} omits the loop variable and defaults to $_
$_ The default variable, print is the printed $_ variable.
(6) The Grep,map function is the equivalent of foreach manipulating each element.

VIII. Cyclic control

Last: Exit loop
Next: Execute the next loop, the statement under next in the loop is not executed this time, for the for is to execute the variable self.
Redo: Re-execute this loop, for the for does not execute variable increment, invalid in do statement, not recommended.
Goto: Jump, can not jump to the loop or subroutine, now structured programming is not recommended.

Ix. Marking

Tags can only consist of letters and underscores, typically uppercase, separated by: delimited.
Tag definition: Defined before a loop statement or before a statement (GOTO)
Generally use the inner layer of the nested loop to jump to the outermost, where last is to exit the outermost loop, and the program continues to go down,
Do not jump to the outermost layer again.

Example:

Copy Code code as follows:

#!/usr/bin/perl
Print "Using Tags:";
Label:for ($i =1; $i <5; $i + +) {
For ($j =1 $j <5; $j + +) {
print "$j";
Last LABEL if $i ==3;
}
}
print "\ $i = $i";

Ten, continue statement block

continue{statement}

(1) Immediately after the loop statement, after the loop body execution, the next condition judgment before the execution.
(2) can also be used after a block of statements identified by {}.
(3) does not execute after jumping from last, but next executes.

Xi. single-line conditional statements and circular statements

Conditional statement: The statement is preceded by the condition after. Print $a if $a ==0, but test the condition before executing the statement.
Similarly:

Copy Code code as follows:

Unless:print $a unless ($a ==0);
While:print $a while ($a –==0);

Note: Loop statements do not lose control variables, resulting in a dead loop.
Using the conditional Statement of | |,&&: $a ==0&&print $a;
Open (F, ' file ') | | Die "can ' t open";
Die function: Exits the program after the console standard error output information.
Warn: does not exit the program after the output information, only acts as the alarm function.
$!: Internal variable contains error code.
$@: Internal variable contains error message.

12. Summary

1. Conditional statement:

Copy Code code as follows:

if (expression) {statement}
elsif (expression) {statement}
....
else{statement}

2. Single line statement if (expression);

3.| | or && and forms

4.?: Form

5.unless (expression) {statement}
Single-line statement unless (expression);

6.while until cycle

while (expression) {statement}
do{statement}while (expression)
while (expression) {statement}continue{statement}
Single-line statement while (expression)
Until (expression) {statement}
do{statement}until (expression)
Until (expression) {statement}continue{statement}
Single-line statement until (expression)

7.for foreach Loop

for (expression 1; expression 2; expression 3) {statement}
foreach (@a) {statement}
foreach $k (keys%hash) {statement}

8. Cycle Control: Last,next,redo

9. Tags: goto LABEL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.