Hello Anonymous Monk,
I can see that everyone has all ready proposed answers and probably all of them are working. I just could not resist to the idea of trying also to create my own answer since I think it a useful topic comparing arrays.
For my answer I use Data::Compare module which provides you which can provide you with several different outputs when you compare arrays. I have also combine my answer with a perlre (regular expression) taken from Regex To Remove File Extension which makes your solution more generic and not only to *.txt extensions.
I would recommend to modify the title to something more appropriate (e.g. Comparing arrays with regex). This is because it produces confusion to people in future that are looking of simply comparing arrays without file extensions etc. etc.
Having said that, sample of working code with output:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
sub array_utils {
use Array::Utils qw(:all);
my @compare = ();
my @account = qw( 96329XY 7776fc 334457yg 2233oop 000001 8877666b)
+;
my @file = qw(8877666b.txt 2233oop.txt 334457yg.txt 7776fc.txt 96
+329XY_K.txt);
foreach my $sample (@file) {
$sample =~ s/(.+)\.[^.]+$/$1/;
push @compare,$sample;
}
# symmetric difference
return my @diff = array_diff(@account, @compare);
# intersection
#return my @isect = intersect(@account, @compare);
# unique union
#return my @unique = unique(@account, @compare);
# check if arrays contain same members
if ( !array_diff(@account, @compare) ) {
return "Arrays contain same members!";
}
# get items from array @account that are not in array @compare
#return my @minus = array_minus( @account , @compare );
}
my @final = array_utils();
print Dumper(\@final);
__END__
$VAR1 = [
'96329XY',
'000001',
'96329XY_K'
];
Play around by removing the # symbols and observe the output, maybe one of them fits more to your needs.
Seeking for Perl wisdom...on the process of learning...not there...yet!
|