On 2012-02-26 21:52, Shawn H Corey wrote:
> On 12-02-26 09:30 PM, Steve Bertrand wrote:
>> I know this isn't a beginner's question, but I know there are geniuses
>> here. Is there a way to simplify this within Perl?
>
> There is no simplify way of doing this. Separate off the first attribute
> and cross-product it with a recursive call to the rest. Like this:
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
>
> my $attributes =
> [
> { type => 'colors', values => [qw/red green blue/] },
> { type => 'sizes', values => [qw/small large/] },
> { type => 'shades', values => [qw/light dark/] },
> ];
>
> sub combo {
> my @traits = @_;
>
> # nothing given, nothing returned
> return if @traits == 0;
>
> # return the last list
> if( @traits == 1 ){
> return map { [ $_ ] } @{ $traits[0]{values} };
> }
>
> # get the combos for everything but the first one
> my @combos = combo( @traits[ 1 .. $#traits ] );
>
> # now combine them
> my @result = ();
> for my $first ( @{ $traits[0]{values} } ){
> for my $rest ( @combos ){
> push @result, [ $first, @$rest ];
> }
> }
>
> return @result;
> }
>
> my @list = combo( @$attributes );
>
> for my $set ( @list ){
> print "@$set\n";
> }
Tres Bien!!! Merci Beaucoup!!
Thank you so much :)
I will pour over this code until I completely understand it. I knew I
came to the right place!
Steve
Thread Previous