简体   繁体   中英

Compare two text files in Perl

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.

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";

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;
        }
    }
}

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"; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM