Reputation: 147
I have several strings in two .txt file. Both of them contain some similar strings but are not arranged on the same line number.
For example, file1.txt Carmen Edison Molly Jason Damon Gerard
file2.txt Edison Jason
I want to save the similar strings that found in both text files (in this case: Edison Jason) to an array.
Upvotes: 0
Views: 201
Reputation: 1172
you could do it this way without installing any additional modules
#!/usr/bin/perl
use warnings;
use strict;
my (@file1,@file2,@match);
open FILE1, "<", 'file1.txt';
@file1 = <FILE1>;
close FILE1;
open FILE2, "<", 'file2.txt';
@file2 = <FILE2>;
close FILE2;
chomp (@file1,@file2);
foreach(@file1) {
if ($_ ~~ @file2) {
push @match, $_;
}
}
print "\nMatches\n";
foreach(@match) { print "The same -- $_\n"; }
Upvotes: 0
Reputation: 1246
Without the need to use additional modules
#!/usr/bin/env perl
use strict;
use warnings;
my @data1 = qw( Carmen Edison Molly Jason Damon Gerard );
my @data2 = qw( Edison Jason );
my @data3 = ();
foreach my $d1 ( @data1 )
{
chomp $d1;
foreach my $d2 ( @data2 )
{
chomp $d2;
if( $d1 eq $d2 )
{
print "Match Found : $d1, $d2 \n";
push @data3, $d1;
}
}
}
Upvotes: 0
Reputation: 2953
There are various array utility libraries for achieving this - what your after specifically is intersection, Array::Utils being one of the simpler libraries to achieve this;
#!/usr/bin/env perl
use strict;
use warnings;
use File::Slurp qw(read_file);
use Array::Utils qw(intersect);
my $file1 = 'file1.txt';
my $file2 = 'file2.txt';
my @data1 = split( /\s/, read_file($file1) );
my @data2 = split( /\s/, read_file($file2) );
my @intersect = intersect( @data1, @data2 );
print join(', ', @intersect), "\n";
Or, without requiring Array::Utils
#!/usr/bin/env perl
use strict;
use warnings;
my @data1 = qw( Carmen Edison Molly Jason Damon Gerard );
my @data2 = qw( Edison Jason );
sub intersect {
my %e = map { $_ => undef } @{$_[0]};
return grep { exists( $e{$_} ) } @{$_[1]};
}
my @intersect = intersect( \@data1, \@data2 );
print join(', ', @intersect), "\n";
Upvotes: 2