Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

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

Help for this page

Select Code to Download


  1. or download this
    use feature 'say';
    use List::MoreUtils 'part';
    ...
    @array = (1, 2, 3, 4, 2, 1, 2, 0, 1, 0, 0);
    
    say join ' ', map { @$_ } part { !!$_ } @array;
    
  2. or download this
    0 0 0 1 2 3 4 2 1 2 1
    
  3. or download this
    (define array (list 1 2 3 4 2 1 2 0 1 0 0))
    
    ...
                  (partition (lambda (x) (< x 1)) 
                             array)])
      (flatten (list x y)))
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2024-04-19 00:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found