I agree with Pustular Postulant that the code can be slightly simplified and that it is beneficial to clearly define the base case. In essence this problem is simply one of data structure traversal. I think you can take advantage of the fact that each hash entry is an array to simplify the problem to a 2-dimensional array traversal. Here's my code. No better than above, but a slightly different approach. I extracted the data structure to a global to simplify the code in order to highlight the traversal logic.

#!/usr/bin/perl use strict; my %set = ( 'foo' => [1, 2, 3], 'bar' => ["A", "B", "C"], 'biz' => ["Y", "Z"], ); my @keys = sort keys %set; my $rows = @keys - 1; my @RESULTSET = (); sub enumerate { my $row = shift; my @result = @_; my @row_data = @{$set{$keys[$row]}}; foreach my $data (@row_data) { # Base Case if ($row == $rows) { my $item; my @copy = (@result, $data); for (my $i; $i < @copy; $i++) { $item->{$keys[$i]} = $copy[$i]; } push @RESULTSET,$item; } # Recursive Case enumerate($row+1, (@result, $data)) if ($row < $rows); } } enumerate(0,()); foreach my $item (@RESULTSET) { foreach my $key (@keys) { print $key ." => ". $item->{$key} .", "; } print "\n"; }

In reply to Re^2: enumeration, again... by satchm0h
in thread enumeration, again... by cazz

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.