http://www.perlmonks.org?node_id=24237


in reply to How can I improve this?

One thing you could do is simply generate all permutations, and then sort them. i.e.

@array = magical_permutation_generating_sub; @sorted_array = sort srtsub @array; sub srtsub { #strip out everything that's not a zero, making it easy #to compare the number of zeros ($A = $a) =~ s/[^0]//; ($B = $b) =~ s/[^0]//; return ($A gt $B); }
This presumes that your only sorting criteria is the number of zeros. But if you have more, just add them to srtsub.

Good luck,
Mark

p.s. goshdarnit lhoward beat me to it and posted better code. I'm gonna leave mine up though cause i think its a little easier for somebody who (i gather) has a C background to grok.