Actually, I think I have a bug. Let me look a little closer. Yes, I have a bug. I moved the declaration of the variables into the wrong scope. This should work a little better.
use strict;
use warnings;
my @array1 = qw( c d e );
my @array2 = qw( e f g h );
my @array3 = qw( a b d );
my @array4 = qw( s g h j k l );
open my $out, '>', 'output' or die "Cannot open: $!\n";
foreach my $a1 ( \@array1, \@array2, \@array3, \@array4 ) {
foreach my $a2 ( \@array1, \@array2, \@array3, \@array4 ) {
my ( %union, %intersect );
foreach my $item1 ( @$a1 ) {
foreach my $item2 ( @$a2 ) {
# print "item1: $item1\nitem2: $item2\n";
$union{$item1}++;
$union{$item2}++;
if ( $item1 eq $item2 ) {
$intersect{$item1}++;
}
}
}
my @intersect = sort keys %intersect;
print $out "@intersect\n"; #prints intersecting words
print $out scalar @intersect . "\n";
}
}
close $out or die "Error closing output; $!\n";