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.