"be consistent" | |
PerlMonks |
Re: how do foreach and while affect an array?by ikegami (Patriarch) |
on Sep 12, 2004 at 17:13 UTC ( [id://390447]=note: print w/replies, xml ) | Need Help?? |
Neither foreach (aka for), while nor do..while change the contents. But what's in the these loops can. If you modify the variable of foreach (or $_ if a variable isn't specified) as shown in the following code, it will affect that element of the array. This is done very commonly.
The following is a example of a strategy sometimes used with while:
In case you're looking for ways to modify the array, look at map, grep and sort. If you assign the value they return back to the original array, it's going to be modified. For example:
In Section
Seekers of Perl Wisdom
|
|