For a while now, I keep running into cases where I need to do enumeration of configuration sets. Most of the enumeration examples I've found are implemented as two dimensional matrixes. Well, this is my quick hack of an implementation to handle multi-dimensional sets. It eats memory, but my sets are rather small.
sub enumerate { my (%p) = @_; my @types = (keys %p); my $type = shift(@types); my @sub_result; if (@types) { my %x; foreach my $subtype (@types) { $x{$subtype} = $p{$subtype}; } (@sub_result) = enumerate(%x); } my @results; foreach my $data (@{$p{$type}}) { if (@sub_result) { foreach my $result (@sub_result) { my %x; foreach my $y (keys %$result) { $x{$y} = $result->{$y}; } $x{$type} = $data; push(@results, \%x); } } else { push (@results, { $type => $data }); } } return (@results); } my @sets = enumerate( "foo" => [1, 2, 3], "bar" => ["A", "B", "C"], "biz" => ["Y", "Z"], ); foreach my $set (@sets) { my @out; foreach my $key (sort keys %{ $set }) { push (@out, "$key => $set->{$key}"); } print join(", ", @out) . "\n"; }
The output is:
[bmc@blah bmc]$ perl /tmp/test.pl |sort bar => A, biz => Y, foo => 1 bar => A, biz => Y, foo => 2 bar => A, biz => Y, foo => 3 bar => A, biz => Z, foo => 1 bar => A, biz => Z, foo => 2 bar => A, biz => Z, foo => 3 bar => B, biz => Y, foo => 1 bar => B, biz => Y, foo => 2 bar => B, biz => Y, foo => 3 bar => B, biz => Z, foo => 1 bar => B, biz => Z, foo => 2 bar => B, biz => Z, foo => 3 bar => C, biz => Y, foo => 1 bar => C, biz => Y, foo => 2 bar => C, biz => Y, foo => 3 bar => C, biz => Z, foo => 1 bar => C, biz => Z, foo => 2 bar => C, biz => Z, foo => 3 [bmc@blah bmc]$

In reply to 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.