cs08 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I need help. I would like to write a programme in Perl which will read (N) number of input files and generate different combinations of data For e.g.
File1 (A B C D ) File2 (P Q R) File3 (1 2 3 4 5)
The output should be like
A,P,1 A,P,2 A,P,3 A,P,4 A,P,5 A,Q,1 A,Q,2 A,Q,3 A,Q,4 A,Q,5 A,R,1 A,R,2 A,R,3 A,R,4 A,R,5 B,P,1 B,P,2 B,P,3 B,P,4 B,P,5
I have tried to generate this using an arrays, but my logic is not taking care of all combinations. Thanks in advance!

20080729 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Generate Matrix in perl
by almut (Canon) on Jul 28, 2008 at 16:11 UTC

    You could also use glob:

    my @set = glob "{A,B,C,D}{P,Q,R}{1,2,3,4,5}"; print "@set\n";

    ___

    $ ./700577.pl AP1 AP2 AP3 AP4 AP5 AQ1 AQ2 AQ3 AQ4 AQ5 AR1 AR2 AR3 AR4 AR5 BP1 BP2 BP +3 BP4 BP5 BQ1 BQ2 BQ3 BQ4 BQ5 BR1 BR2 BR3 BR4 BR5 CP1 CP2 CP3 CP4 CP5 CQ +1 CQ2 CQ3 CQ4 CQ5 CR1 CR2 CR3 CR4 CR5 DP1 DP2 DP3 DP4 DP5 DQ1 DQ2 DQ3 DQ +4 DQ5 DR1 DR2 DR3 DR4 DR5

      You can even add the comma into the glob, so the result is more like the one requested.

      $ perl -e '$,=$\=$/; print glob( "{A,B},{1,2},z" );' A,1,z A,2,z B,1,z B,2,z $

      Sometimes I wonder, why I usually avoid glob...

      edit: minor code changes

Re: Generate Matrix in perl
by moritz (Cardinal) on Jul 28, 2008 at 15:57 UTC
Re: Generate Matrix in perl
by olus (Curate) on Jul 28, 2008 at 15:53 UTC

    Don't be afraid to show us what you have tried so far. Everyone will help you in understanding what it is that you are doing wrong and to point you in the right direction

Re: Generate Matrix in perl
by pjotrik (Friar) on Jul 28, 2008 at 16:18 UTC
    I'll skip the data reading... With the data in memory, it would go like this:
    #!/usr/bin/perl use warnings; use strict; my $src = [['A', 'B', 'C', 'D'],['P', 'Q', 'R'],['1', '2', '3', '4', ' +5']]; my $products = ['']; for my $level (@$src) { my $old_products = $products; $products = []; for my $prefix (@$old_products) { for (@$level) { push(@$products, "$prefix$_"); } } } print "$_\n" for @$products;
    Update: That's merely to help your logic. From the practical view, advices from almut and moritz are probably better.
      Thanks Guys! I could able to resolve it using Set::CrossProduct Thanks again!