Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Printing an array using while loop

by AnomalousMonk (Archbishop)
on May 19, 2020 at 23:18 UTC ( [id://11116954]=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

If order is important, pop is wrong in both examples:

c:\@Work\Perl\monks>perl -wMstrict -le "my @array1 = qw/ball bat helmet/; print qq{\@array1 before: (@array1)}; ;; my @array2; while (@array1) { my $item = pop @array1; print $item; push(@array2,$item); } print qq{\@array2 after: (@array2)}; " @array1 before: (ball bat helmet) helmet bat ball @array2 after: (helmet bat ball)
shift does the trick:
c:\@Work\Perl\monks>perl -wMstrict -e "use Data::Dump qw(dd); ;; my @array1 = (qw/ball bat helmet/, 0, undef); dd '@array1 before:', \@array1; ;; my @array2; while (@array1) { my $item = shift @array1; dd $item; push(@array2,$item); } dd '@array2 after:', \@array2; " ("\@array1 before:", ["ball", "bat", "helmet", 0, undef]) "ball" "bat" "helmet" 0 undef ("\@array2 after:", ["ball", "bat", "helmet", 0, undef])


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (None)
    As of 2024-04-25 01:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found