Perl in foreach (i)

Source: Internet
Author: User

The foreach structure in perl first syntax foreach $rock (qw/bedrock slate lava/) {$rock = "\t$rock"; $rock. = "\ n";}Foreach iterates from the first element of a list (array) to the last element, one iteration at a time. The control variable (in this case, $rock) takes a new value out of the list each time it is iterated. The first time is "bedrock" and the third time is "lava". Control variables are not a copy of these list elements but rather the elements themselves . That is, if you modify the variable in a loop, the elements in the original list are also modified, as shown in the following code snippet. This nature is useful, but if it is unclear, it may surprise the result.
What is the value of $rock at the end of the loop? The value is the same as before the loop begins. The value of the control variable in the Foreach loop is automatically saved and restored by Perl. When the loop is in progress, there is no way to change its value. At the end of the loop, the value of the variable goes back to the beginning of the loop and undef if there is no value. This means that if there is a variable and control variable with the same name: "$rock", do not worry about confusing them. Note: 1, the loop variable in other languages is usually a copy of the loop element, changing the value of the loop variable does not change the value of the original element. Perl, however, has a cyclic variable that points to the physical address of the loop element, so changes to the loop variable are changes to the original loop element. 2, when the loop ends, the value of the $rock (the loop variable) reverts to the value before the start of the loop, which is another place that differs from other languages.#! /usr/bin/perl-w
Use strict;
My @array = (1.. 9);
My $num;
foreach $num (@array) {$num **= 2;
}
print "@array \ n";
The output is as follows:
1 4 9 16 25 36 49 64 81
The important point here is that the control variable $num represents a specific item in the @array. Modifying the value of a control variable within the body of a foreach structure alters the @array element currently represented by the control variable.
Therefore, the current value of the @array changes every time the code inside the foreach is executed

Perl in foreach (i)

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.