in reply to move all 0s in an array to the beginning keeping other elements order same
This is a fun little TIMTOWTDI question :-) Here's another solution:
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"
|
|---|