Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^2: Printing an array using while loop

by ikegami (Pope)
on Oct 22, 2007 at 05:58 UTC ( #646366=note: print w/replies, xml ) Need Help??

in reply to Re: Printing an array using while loop
in thread Printing an array using while loop

You've already been shown how to make your code work with all values including undef.

while (@array) { my $item = shift(@array); print "$item\n"; }

If you wanted safe code that didn't refer to the array twice, you could use splice in a list assignment.

while (my ($item) = splice(@array, 0, 1)) { print "$item\n"; }

The parens on the LHS of the assignment are crucial to force a list context. The result of a list assignment in list context is the number of list elements assigned (no matter if the element(s) are true or false).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://646366]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2019-08-23 15:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found