I have 5 arrays I would like to compare but would like to do it a pair at a time. I initially had gotten help by seeing if I can have a variable in an array. My arrays were: @array1, @array2, @array3, @array4. I am able to compare 2 at a time manually with:
use strict;
use warnings;
@array1 = (c,d,e);
@array2 = (e,f,g,h);
@array3 = (a,b,d);
@array4 = (s,g,h,j,k,l)
my (%union, %intersect);
foreach my $e (@array1, @array2) {
$union{$e}++ && $intersect{$e}++
}
my @intersect = sort keys %intersect;
print FILEOUT "@intersect\n"; #prints intersecting words
print FILEOUT scalar @intersect;
I thoutht I could change the array names to variable so that I can have the script run through all pairs. The idea was this; through a loop change the array name so that they could be compared. Name change would be done like this:
foreach (1..4){@{"array$_"};}
I started by testing it out like this:
foreach(1..4) {
my (%union, %intersect);
foreach my $e (@title1, @{"title$_"}) {
$union{$e}++ && $intersect{$e}++
}
my @intersect = sort keys %intersect;
print FILEOUT "@intersect\n";
print FILEOUT scalar @intersect;
print FILEOUT "\n";
}
It wasn't working. For some reason the comparison doesn't work and my results were four 0s. I thought it may have been where I declared the variables and moved it out of the loop but no luck.
Wondering if anyone had any ideas or could help. My end result will be a table like this:
array1 : array2 : array3 : array4
array1: 10 : 5 : 17 : 2
array2: 5 : 15 : 8 : 1
array3: 17 : 8 : 14 : 6
array4: 2 : 1 : 6 : 19
Thought I'd start with the comparison first and then format it. I think as I loop through it, I can control the layout.
Thanks-