Greetings,
Nothing novel but this question looked like fun so here are my 2 cents. I will leave the Benchmark tests to those more versed in such things :}
#!/usr/bin/perl -w use strict; use Data::Dumper; # The HoA below may come in varying # sizes: from "key1" to "key 50" my $hash = { 'key1' => [ 1, 2, 3, 4 ], 'key2' => [ 10, 20, 30 ], 'key3' => [ 100, 200, 300 ], 'key4' => [ 'a','b','c','dood'] }; my (@b, @base); #compute combos for(sort keys %{$hash}){ concat_arrays(\@b, $hash->{$_}); } #filter computations @base = grep{ my $count = () = $_ =~ m/\-/g; ($count == (scalar(keys %{$hash})-1)); }sort @base; #double check print $_."\n" for(@base); sub concat_arrays { my ($baseref, $newref) = @_; foreach my $new (@{$newref}){ if(scalar(@{$baseref})){ foreach my $base (@{$baseref}){ push @base, $base." - ".$new; } }else{ push @base, $new; } } @b = @base; }
The output
1 - 10 - 100 - a 1 - 10 - 100 - b 1 - 10 - 100 - c 1 - 10 - 100 - dood 1 - 10 - 200 - a 1 - 10 - 200 - b 1 - 10 - 200 - c 1 - 10 - 200 - dood 1 - 10 - 300 - a 1 - 10 - 300 - b 1 - 10 - 300 - c 1 - 10 - 300 - dood 1 - 20 - 100 - a 1 - 20 - 100 - b 1 - 20 - 100 - c 1 - 20 - 100 - dood 1 - 20 - 200 - a 1 - 20 - 200 - b 1 - 20 - 200 - c 1 - 20 - 200 - dood 1 - 20 - 300 - a 1 - 20 - 300 - b 1 - 20 - 300 - c 1 - 20 - 300 - dood 1 - 30 - 100 - a 1 - 30 - 100 - b 1 - 30 - 100 - c 1 - 30 - 100 - dood 1 - 30 - 200 - a 1 - 30 - 200 - b 1 - 30 - 200 - c 1 - 30 - 200 - dood 1 - 30 - 300 - a 1 - 30 - 300 - b 1 - 30 - 300 - c 1 - 30 - 300 - dood 2 - 10 - 100 - a 2 - 10 - 100 - b 2 - 10 - 100 - c 2 - 10 - 100 - dood 2 - 10 - 200 - a 2 - 10 - 200 - b 2 - 10 - 200 - c 2 - 10 - 200 - dood 2 - 10 - 300 - a 2 - 10 - 300 - b 2 - 10 - 300 - c 2 - 10 - 300 - dood 2 - 20 - 100 - a 2 - 20 - 100 - b 2 - 20 - 100 - c 2 - 20 - 100 - dood 2 - 20 - 200 - a 2 - 20 - 200 - b 2 - 20 - 200 - c 2 - 20 - 200 - dood 2 - 20 - 300 - a 2 - 20 - 300 - b 2 - 20 - 300 - c 2 - 20 - 300 - dood 2 - 30 - 100 - a 2 - 30 - 100 - b 2 - 30 - 100 - c 2 - 30 - 100 - dood 2 - 30 - 200 - a 2 - 30 - 200 - b 2 - 30 - 200 - c 2 - 30 - 200 - dood 2 - 30 - 300 - a 2 - 30 - 300 - b 2 - 30 - 300 - c 2 - 30 - 300 - dood 3 - 10 - 100 - a 3 - 10 - 100 - b 3 - 10 - 100 - c 3 - 10 - 100 - dood 3 - 10 - 200 - a 3 - 10 - 200 - b 3 - 10 - 200 - c 3 - 10 - 200 - dood 3 - 10 - 300 - a 3 - 10 - 300 - b 3 - 10 - 300 - c 3 - 10 - 300 - dood 3 - 20 - 100 - a 3 - 20 - 100 - b 3 - 20 - 100 - c 3 - 20 - 100 - dood 3 - 20 - 200 - a 3 - 20 - 200 - b 3 - 20 - 200 - c 3 - 20 - 200 - dood 3 - 20 - 300 - a 3 - 20 - 300 - b 3 - 20 - 300 - c 3 - 20 - 300 - dood 3 - 30 - 100 - a 3 - 30 - 100 - b 3 - 30 - 100 - c 3 - 30 - 100 - dood 3 - 30 - 200 - a 3 - 30 - 200 - b 3 - 30 - 200 - c 3 - 30 - 200 - dood 3 - 30 - 300 - a 3 - 30 - 300 - b 3 - 30 - 300 - c 3 - 30 - 300 - dood 4 - 10 - 100 - a 4 - 10 - 100 - b 4 - 10 - 100 - c 4 - 10 - 100 - dood 4 - 10 - 200 - a 4 - 10 - 200 - b 4 - 10 - 200 - c 4 - 10 - 200 - dood 4 - 10 - 300 - a 4 - 10 - 300 - b 4 - 10 - 300 - c 4 - 10 - 300 - dood 4 - 20 - 100 - a 4 - 20 - 100 - b 4 - 20 - 100 - c 4 - 20 - 100 - dood 4 - 20 - 200 - a 4 - 20 - 200 - b 4 - 20 - 200 - c 4 - 20 - 200 - dood 4 - 20 - 300 - a 4 - 20 - 300 - b 4 - 20 - 300 - c 4 - 20 - 300 - dood 4 - 30 - 100 - a 4 - 30 - 100 - b 4 - 30 - 100 - c 4 - 30 - 100 - dood 4 - 30 - 200 - a 4 - 30 - 200 - b 4 - 30 - 200 - c 4 - 30 - 200 - dood 4 - 30 - 300 - a 4 - 30 - 300 - b 4 - 30 - 300 - c 4 - 30 - 300 - dood


-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

In reply to Re: Creating a Dynamic Nested Loops for HoA by injunjoel
in thread Creating a Dynamic Nested Loops for HoA by monkfan

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.