Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi Midget

as noted above, I have suggested yesterday four nested foreach loops in my answer to your cross post on the DevShed forum:

foreach my $s1 (@set1) { foreach my $s2 (@set2) { foreach my $s3 (@sizes) { foreach my $s4 (@colors) { print "$s1$s2$s3$s4\n"; } } } }

And I believe it is probably the best solution: clean, easy to read, easy to understand and efficient.

For the fun of it, however, let me suggest a kind of "Schwartzian" solution, which could also be called a "Lisp written in Perl" solution (or Scheme, or Haskell, or whatever functional language written in Perl):

my @sku = qw/SKU1 SKU2 SKU3/; my @tslh = qw/t s l h/; my @colors = qw/BLU GRN WHT BLK /; my @sizes = qw / S M L XL 2X /; print map { $col = $_; map { $siz = $_; map {$a1 =$_; map "$_ $a1 $siz + $col\n", @sku;} @tslh} @sizes } @colors;

(I have added spaces between the fields to make the output easier to read.) This prints (I copy only the beginning and the end of the 240-line printout):

SKU1 t S BLU SKU2 t S BLU SKU3 t S BLU SKU1 s S BLU SKU2 s S BLU ... SKU3 l 2X BLK SKU1 h 2X BLK SKU2 h 2X BLK SKU3 h 2X BLK

The beauty (and fun) of this solution is of course that it holds in just one line of code (in pure Perl, without using a module to do the work behind the scene).

There are a number of downsides to this solution, however: first, it is far less easy to understand, it is actually hardly readable. Even though I just wrote it, I am not sure how I would explain it to make it clearly understandable.

To tell the truth, it was also a bit complicated for me to write it: it took me about 3 minutes to write the foreach nested loops above (which I did not even need to test), and and it took me slightly more than half an hour to get the nested map function calls to work correctly. So, in brief, this was just for the fun, I do not recommend this solution.


In reply to Re: Multiple Permutation handling by Laurent_R
in thread Multiple Permutation handling by midget2000x

Title:
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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    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: (10)
    As of 2014-09-02 23:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (34 votes), past polls