Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Definitely TIMTOWTDI, but so far everyone agrees it's better (at least easier) to generate the permutations first and then sort the list, rather than trying to do both at once. Take a look at the Perl Cookbook section 4.15 (if you have it) for info on sorting lists based on a comparison function; it also has some effiency hints. I didn't do it by strings like the other Monks (which I realize differs from the form of your question), but this might be more efficient, and you can always regenerate the strings at the end. Here's what I came up:

#!/usr/bin/perl -w use strict; my (@a, @b, @c, @d, @e, $i1, $i2, $i3, $i4, $i5); @a = @b = @c = @d = @e = (0, 1, 2); # or whatever my (@unsorted, @sorted); for ($i1 = 0; $i1 <= $#a; $i1++) { for ($i2 = 0; $i2 <= $#b; $i2++) { for ($i3 = 0; $i3 <= $#c; $i3++) { for ($i4 = 0; $i4 <= $#d; $i4++) { for ($i5 = 0; $i5 <= $#e; $i5++) { push @unsorted, [$i1, $i2, $i3, $i4, $i5]; } } } } } @sorted = sort { non_zeros($b) <=> non_zeros($a) || ${$b}[0] <=> ${$a}[0] || ${$b}[1] <=> ${$a}[1] || ${$b}[2] <=> ${$a}[2] || ${$b}[3] <=> ${$a}[3] || ${$b}[4] <=> ${$a}[4] } @unsorted; print map "@$_\n", @sorted; sub non_zeros { my @arr = @{$_[0]}; scalar grep { $_ != 0 } @arr; }

I'm not a big fan of any of the permutation generators (including mine) listed so far; I'll try to think of something cleaner and more general.

In reply to Re: How can I improve this? by athomason
in thread How can I improve this? by Dogg

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (2)
    As of 2015-10-04 05:12 GMT
    Find Nodes?
      Voting Booth?

      Does Humor Belong in Programming?

      Results (99 votes), past polls