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).
|
|---|