Perl how does foreach work




















You can specify that list directly in the parentheses after foreach , use an array variable, or use the result of a subroutine call amongst other ways to get a list :. Some people like to use the synonym for. The aliasing is only temporary.

After the foreach the topic variable returns to its original value:. This is an alias instead of a copy, which is a shortcut that allows your program to be a little faster by not moving data around. Not only that, but if you change the source by adding or removing elements you can screw up the foreach.

This loops infinitely processing the same element because each go through the block moves the array elements over one position; when the iterator moves onto the next position it finds the same one it just saw:. You can use your own name by specifying a scalar variable between the foreach and the parentheses. Since Perl flattens lists into one big list, you can use more than one list source in the parentheses:.

The output shows the aliasing effect and that the original value is restored after the foreach :. There are three keywords that let you control the operation of the foreach and other looping structures : last , next , and redo. The last stops the current iteration.

It does not look at the next item. You often use this with a postfix conditional:. While this seems easier, I prefer to the Perl foreach loop to the for loop see " Perl best practice - prefer Perl foreach loop to for loop " , especially if you don't use Perl every day.

There's also another way to use a Perl for loop, and this is the one area where I like the for loop syntax:. When using this type of Perl for loop, you are often not working with an array, but are working in a loop for some other reason.

Any other time you are working with a Perl array, I prefer the earlier foreach or for loop syntax. This example is similar to the previous for loop example, where you have a counter, and you're incrementing that counter while doing something else in the loop. In this case I'm just printing a string, but in your code you'll be doing something more complex.

The Perl while loop is used more often when iterating over a Perl hash. For instance, in the following example, I first create a simple Perl hash, and then I iterate over the hash with a while loop:.

Active Oldest Votes. Improve this answer. Thanks ikegami for replying. Just to learn how it works or something else? That's a stupid premise. If there were no operators, you wouldn't be able to do anything at all. If you were specifically referring to named operators, you be limited to writing programs that couldn't produce any output but runtime errors.

Not useful. Sign up or log in Sign up using Google. Sign up using Facebook. In Perl, the for and foreach loop are interchangeable, therefore, you can use the foreach keyword in where you use the for keyword. The following example uses the Perl for loop statement to loop over elements of an array :.

If you replace the for keyword by the foreach keyword in the above example, it works the same. If you want to specify an explicit iterator for the loop, you can declare it in the for loop statement as follows:. If you declare an iterator before entering the loop, Perl will restore its original value after the loop terminated.



0コメント

  • 1000 / 1000