I need to compare two array and get differences.
Background:
1st array will list out files in folder.
2nd array will read the content of a file and stored in array.
The output of 1st array will be
a
b
c
d
e
The output of 2nd array will be
a
b
c
e
How I can compare those 2 array that get differences? The finale output that I want is
d
Here is the code:
#!/usr/bin/perl
use strict;
use warnings;
my $list = "experiment.sv";
my $path = "../../../folder1/";
my $filelist;
open ( OUTFILE, ">output.txt" );
main ();
close OUTFILE;
sub main {
my @array1;
opendir ( DIR, $path ) || die "Error in opening dir $path\n";
while ( $filelist = readdir (DIR) ) {
next if ( $filelist =~ s/\.//g); #/
push @array1, $filelist;
}
closedir(DIR);
my @array2;
open( my $fh, "<", "$path/$list") or die "Failed to open file: $!\n";
while(<$fh>) {
push @array2, $_;
}
close $fh;
my @result;
foreach my $array2 (@array2) {
foreach my $array1 (@array1) {
if ($array1 !~ /$array2/ ) {
push @result, "$array1\n";
}
}
}
print OUTFILE "",@result;
}