I second your choice of recursion to solve this problem, but I think that the code can be simplified significantly without much loss in readability. In particular, when writing recursive functions I try to make the "base case" and the "general case" (and in particular, the "recursive step") as easy to identify as possible. Also note that Perl makes it very easy to copy hashes in one fell swoop; no need to copy the key/value pairs individually:
sub enumerate { # base case: return an array containing a single # empty anonymous hash (ref). return ( +{} ) unless @_; # general case my %p = @_; my ( $key, $val ) = each %p; my @ret; delete $p{ $key }; for my $h ( enumerate( %p ) ) { # recursion for my $d ( @$val ) { my %c = %$h; # copy hash $c{ $key } = $d; push @ret, \%c; } } return @ret; }
the lowliest monk
In reply to Re: enumeration, again...
by tlm
in thread enumeration, again...
by cazz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |