Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Creating combinations using arrays

by DreamT (Pilgrim)
on Mar 05, 2013 at 16:21 UTC ( #1021862=perlquestion: print w/replies, xml ) Need Help??
DreamT has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have a product feed with a number of properties (size, shape, colour, etc.). Each property has values (size has S,M,L etc., shape has circle, square etc., colour has red,white, blue, etc.) I.e., both the properties and values are variable.

Based on these I want to create all possible combinations, for example "Blue circle size S","Blue square size S" etc, but I don't know how to do it. For now I'm storing in an HoA:
my %Properties; ... # Outer loop, per $property_id # Inner loop, per value of property: push @{ $Properties->{$property_id} }, { 'Value' => $value, 'Price' => $price, };
Ideally, I want to loop the Properties-hash and create the combinations from that. I know how to do it in a static fashion (Defining a number of arrays and looping them), but how to do it dynamically?

Replies are listed 'Best First'.
Re: Creating combinations using arrays
by Kenosis (Priest) on Mar 05, 2013 at 16:47 UTC
      Thank you!
      No, this is a real-world problem that needs to be solved :-)
Re: Creating combinations using arrays
by blue_cowdawg (Monsignor) on Mar 05, 2013 at 16:30 UTC
        Based on these I want to create all possible combinations,

    Pretty simple actually...

    my @colors = qw/ ***insert colors here*** /; my @sizes = qw/ **** insert sizes here ***/ ; my @shapes= qw/ *** insert shapes here ***/; my @combos=(); foreach my $color(@colors){ foreach my $size(@sizes) { foreach my $shape(@shapes) { push @combos,{ color => $color, size => $size, shape => $shape}; } } }


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Well, the problem is that i don't know which properties that are available at the time, so instead of @colors, @sizes, @shapes, I need something more dynamic.
        > Well, the problem is that i don't know which properties that are available at the time, so instead of @colors, @sizes, @shapes, I need something more dynamic.

        So use a recursive function!

        rec_for([1..3],["a".."c"],[10..13]); my @tuple; sub rec_for { if (@_) { my $a_list = shift; for my $x (@$a_list) { push @tuple, $x; rec_for(@_); pop @tuple; } } else { print "tuple: @tuple\n"; } }

        /usr/bin/perl -w /home/lanx/B/PL/PM/recursive_for.pl tuple: 1 a 10 tuple: 1 a 11 tuple: 1 a 12 tuple: 1 a 13 tuple: 1 b 10 tuple: 1 b 11 ...

        Cheers Rolf

        UPDATE

        if you need the result in a %hash, just alter the routine to process key-value pairs

        rec_for(a=>[1..3],b=>["a".."c"],c=>[10..13]);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1021862]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-10-19 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (106 votes). Check out past polls.

    Notices?