Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by anilmwr (Initiate)
on May 02, 2014 at 11:39 UTC ( [id://1084774]=note: print w/replies, xml ) Need Help??


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

Thank you all!

By "better", I meant maybe using less arrays or like that.

It seems asking Perl questions here is more fruitful than on StackOverflow. :-)

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

Replies are listed 'Best First'.
Re^2: move all 0s in an array to the beginning keeping other elements order same
by sundialsvc4 (Abbot) on May 02, 2014 at 12:04 UTC

    With all due respect to SO, it is definitely the case that PerlMonks is the “go-to site” on the Internet for Perl related questions.   The greatest concentration of [Perl, and other things] expertise that I have ever found is here, and generally quite willing to jump in and help ... with examples ... for even some mighty esoteric questions.

    As others have said, I would solve this problem by using grep to curry out all of the elements that are not zero, then if necessary unshift a list containing an appropriate number of zeros onto the front of the result.   This will have the effect of “moving the zeros to the front” without otherwise altering the order of the elements, although it does so in a different (but equivalent) way.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found