Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: move all 0s in an array to the beginning keeping other elements order same

by Anonymous Monk
on May 01, 2014 at 22:10 UTC ( [id://1084698]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my @array = (1, 2, 3, 4, 2, 1, 2, 0, 1, 0, 0);
    
    @array = do { my @tmp = grep $_, @array; ((0)x(@array-@tmp),@tmp) };
    
    print "@array\n";  # prints "0 0 0 1 2 3 4 2 1 2 1"
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-19 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found