Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Printing an array using while loop

by ikegami (Patriarch)
on Oct 22, 2007 at 05:58 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-03-19 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found