The use of control structures in Perl

Source: Internet
Author: User
Tags foreach stdin

If control structure

Chomp (My $a = <STDIN>);  User input variable $a value
if ($a > 0) {  If $a is greater than 0 executes command
        print "$a is larger than 0\n"; 
} 
elsif ($a = = 0) {  if $a equals 0 then execute command
        print "$a equals 0\n"; 
} 
else{  If $a is less than 0 execute command
        print "$a is smaller than 0\n"; 
}

Unless control structure in contrast to if action, to execute a command when judged false

Chomp (My $a = <STDIN>); 
Unless ($a > 0) { 
        print "$a is not larger than 0\n"; 
}

While control structure

$b = 0;  Assign a value of variable $b 0
while ($b <) {  when $b is less than 20 o'clock execute the command in the
        loop $b + +; 
        Say "$b"; 
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

while (<>) {  always lets the user input string
        if (/fred/) {  Print if the string containing "Fred" is entered, print is not printed
                ; 
        } 
}

Until control structure, which, contrary to the while function, executes the loop when the judgment is false

Until (0) { 
        print ' test '; 
}

foreach Control structure

foreach (1..10) {  The list is a value from 1 to 10 and loops print
        "$_\n"; 
}
foreach $str (Qw/hello bye the you/) {  loops through the list of strings to print
        "$STR \ n"; 
}

For control structure, similar to foreach

For ($i =10 $i >0; $i-) {  variable $i cycles from 10 to 1 and prints print
        "$i \ n"; 
}

This article is from the "Strive for" blog, please be sure to keep this source http://carllai.blog.51cto.com/1664997/1186224

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.