The control structure in Perl learning notes _perl

Source: Internet
Author: User
Tags chop


First, the condition judgment


code as follows:

if () {

}
elsif () {

}
...
else{

}

Another way to build an I-f statement is to use multiple expressions, then, depending on which expression is true, run the code: You can read the above statement block as follows: If the expression labeled E x P r e s i o 1 is true, then the statement block B L o C K 1 runs. Otherwise, the control is transferred to E L S I f, the E x P r e s i o n 2 is tested, if the expression is true,
Then run b L O C K 2. If E x P r e s i o n 1 and E x P r e s i o n 2 are not true, then B L o C K 3 runs





Second, Cycle:



1, while loop


 code as follows:

while () {

}

2, until cycle
 code as follows:

Until () {

}

3, Class C for the loop, such as
 code as follows:

for ($count =1; $count <= 5; $count + +) {
# statements inside the loop go
}

Here is an example of using the comma operator in the For loop:
 code as follows:

for ($line =, $count = 1; $count <= 3; $line =, $count + +) {
Print ($line);
}

It is equivalent to the following statement:
 code as follows:

$line =;
$count = 1;
while ($count <= 3) {
Print ($line);
$line =;
$count + +;
}

4, for the list (array) of each element of the loop: foreach, the syntax is:
 code as follows:

foreach Localvar (listexpr) {
Statement_block;
}

Cases:
 code as follows:

foreach $word (@words) {
if ($word eq "the") {
Print ("found the word ' \ n");
}
}

Note:
(1) Here the loop variable Localvar is a local variable, and if it already has a value, the value is restored after the loop.
(2) Change the local variable in the loop, the corresponding array variable will also change, such as:
 code as follows:

@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
if ($temp = = 2) {
$temp = 20;
}
}

At this point the @list has become (1, 20, 3, 4, 5).
5. Do Loop
 code as follows:

do {
Statement_block
} while_or_until (condexpr);

The Do loop performs at least one loop.
6. Cycle control
The exit loop is last, the same as the break in C; the next loop is the same as the Continue function in C; a Perl-specific command is redo, which means repeating the loop, which is the same as the loop variable, returning to the starting point of the loop, but note that The redo command does not work in the Do loop.
7, the traditional goto label;





Iii. single-line conditions
The syntax is statement keyword CONDEXPR. Where keyword can be if, unless, while, or until, such as:


 code as follows:

Print ("This is zero.\n") if ($var = = 0);
Print ("This is zero.\n") unless ($var!= 0);
Print (' not Zero yet.\n ') while ($var-> 0);
Print ("Not Zero yet.\n") until ($var-= = 0);

Although the conditional judgment is written in the back, it is executed first.





directive: unless if not
The meaning of unless is to say, "If the discriminant is not true, execute ...".
Grammar one:


 code as follows:

Unless (discriminant expression) {
The discriminant is a false-time statement block;
}

The last syntax can also be written in Perl: The discriminant is a false-time statement block unless (discriminant expression);
Example:
 code as follows:

Print "Please enter your score? \ n";
$scorre =; #Stands for standard input, the user is allowed to enter a string
Chop ($score); #将 $score last newline character \ nthe deletion
Unless ($score <60)
{
Print "Your score passed!\n";
}

can also be written: print "Your score passed!\n" unless ($score <60);
Syntax Two:
 code as follows:

Unless (discriminant expression)
{
The discriminant is a false-time statement block;
}else{
Statement block when the discriminant is true;
}

Example:
code as follows:

Print "Please enter your score? \ n";
$scorre =;
Chop ($score);
Unless ($score <60)
{
Print "Your score passed!\n";
}else{
Print "Your score is less than lattice!\n";
}

Advanced Skills: &&,| | and?: As a control structure
They look like punctuation marks, or part of an expression. However, it can be used as a control structure in Perl.
For example:
 code as follows:

if (discriminant expression)
{is a true time statement block};

Can also be written as:
 code as follows:

is true when the statement block if (discriminant expression)

But the simpler way is:
 code as follows:

Statement block with discriminant && true

Why, then? && is the logical AND operator, which means:
If the discriminant is true, the value of the expression depends on the value of the subsequent statement block. So the statement block that is true is executed (for evaluation).
If the discriminant is false, the entire expression is false, regardless of the value of the subsequent statement block. So the statement block is not executed when it is false.
In the same way, unless (this) {that} can be replaced with this| | that.
An example of an expression: EXP1?EXP2:EXP3 says: If Exp1 is true, the value of EXP2 is obtained, otherwise EXP3 value is obtained.




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.