-2

I intend to make program which generates new files, that contain combination of element from multiple array.

First, program prompts user to input name of files.
Every file contains single word per line.
Every file will be treated as an array, and the contents per line is array element.

The program then get all possible combination of those arrays element (files content),
and print every combination to a new file.
So the new file created will be as much as the possible combination.

Example:
file1.txt:
f1a
f1b

file2.txt:
f2a
f2b
f2c

Illustration:

'>perl combine.pl
'>Filename(s) to be combined: file1.txt file2.txt
result:
'>file1.txt = 2 elements.
file2.txt = 3 elements.
Combination success generates 6 files.

At the same directory, program generates 6 files combination result.
result-1.txt
f1a
f2a

result-2.txt
f1a
f2b

result-3.txt
f1a
f2c

result-4.txt
f1b
f2a

result-5.txt
f1b
f2b

result-6.txt
f1b
f2c

.

Here's some code i had:

combine.pl:

#!/usr/bin/perl 
use Data::Dumper;

print "Filename(s) to be combined: ";
$userinput =  <STDIN>; 
chomp $userinput;

my @filenames = split /\s+/, $userinput;

my @arr;
my $i = 0;

foreach (@filenames){
    # open file
    open (my $fn, "<", $_) or die ("can't open file");
    {
        local $/;
        @arr[$i] = <$fn>;
    }
    close ($fn);
    $i++;
}
print "\n".Dumper(@arr);

# get array size
my $arrSize =  scalar @arr;
print "\nSets of array from input: " . $arrSize . "\n";

#die;

that's what i've done so far,
Dynamically get the content of file input,
and put each file content to an array (i expect it will be easier to combine).

I've updated the question and cleaned up the code a bit.
So the question; how to print each cartesian-crossProduct from those input, to new separated files like illustration above.

ask91
  • 45
  • 1
  • 11
  • This smells like homework, and I don't see any attempt to solve the question in the above code. Can you describe how you would get every possible combination of two arrays of elements, then post code attempting to do something with them? – Oesor Feb 05 '14 at 15:13
  • 2
    The way the script ends is hilarious. `print @b; die;` The End :) – Zaid Feb 05 '14 at 15:17
  • I updated a bit of code and explanation above. And yes, CPAN solution amazed me with three lines code, but i wish any idea of solution in conventional way without CPAN :'( – ask91 Feb 05 '14 at 19:48

1 Answers1

2

I'll leave the deciphering as an exercise for the OP until they clarify if it is homework or not. Pass in the filenames as arguments to the following script on the command line:

use strict;
use warnings;
use File::Slurp qw< read_file write_file >;
use List::Gen   qw< cartesian >;

my $counter;
my @slurped_files   = map { my @file = read_file($_); [ @file ] } @ARGV;
my @resultant_files = cartesian { [ @_ ] } @slurped_files;

$counter++, write_file( @$_, "result-$counter.txt" ) for @resultant_files;

Grunt work done in three lines. Gotta love CPAN :)

Zaid
  • 36,680
  • 16
  • 86
  • 155
  • +1, perhaps https://eval.in/98238 (and `my $counter=0;`) – mpapec Feb 05 '14 at 16:38
  • I appreciate you show me how to work it out with CPAN. i attach code i did, even if it's freaking weird because i'm totally new & have to face some condition to code with perl so the code maybe mixed with other language syntax. so, sorry for rude codes, i know i must learn to write code in perl well. But it didn't like a homework i could copy paste – ask91 Feb 05 '14 at 17:17