4.6. Iterating Over an Array by Reference4.6.1. ProblemYou have a reference to an array, and you want to use a loop to work with the array's elements. 4.6.2. SolutionUse foreach or for to loop over the dereferenced array:
4.6.3. DiscussionThe solutions assume you have a scalar variable containing the array reference. This lets you do things like this:
We could have rewritten the foreach loop as a for loop like this:
Frequently, though, the array reference is the result of a more complex expression. Use the @{ EXPR } notation to turn the result of the expression back into an array:
Again, we can replace the foreach with a for loop:
4.6.4. See Alsoperlref(1) and perllol(1); Chapter 8 of Programming Perl; Recipe 11.1; Recipe 4.5
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|