Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 比较Perl中的两个文本文件_Arrays_String_Perl_File_Text - Fatal编程技术网

Arrays 比较Perl中的两个文本文件

Arrays 比较Perl中的两个文本文件,arrays,string,perl,file,text,Arrays,String,Perl,File,Text,我在two.txt文件中有几个字符串。它们都包含一些类似的字符串,但不是排列在同一行号上 比如说,, file1.txt 卡门 爱迪生 莫莉 杰森 达蒙 杰拉德 file2.txt 爱迪生 杰森 我想将两个文本文件中的相似字符串(在本例中为Edison Jason)保存到一个数组中。有各种数组实用程序库可以实现这一点-您特别需要的是交集,array::Utils是实现这一点的较简单库之一 #!/usr/bin/env perl use strict; use warnings; use Fil

我在two.txt文件中有几个字符串。它们都包含一些类似的字符串,但不是排列在同一行号上

比如说,, file1.txt 卡门 爱迪生 莫莉 杰森 达蒙 杰拉德

file2.txt 爱迪生 杰森


我想将两个文本文件中的相似字符串(在本例中为Edison Jason)保存到一个数组中。

有各种数组实用程序库可以实现这一点-您特别需要的是交集,array::Utils是实现这一点的较简单库之一

#!/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";
或者,不需要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";

无需使用其他模块

#!/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;
        }
    }
}
#!/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"; }

您可以这样做,而无需安装任何其他模块

#!/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;
        }
    }
}
#!/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"; }
#/usr/bin/perl
使用警告;
严格使用;
我的(@file1、@file2、@match);

打开文件1,“我们在这里是为了帮助您完成任务,而不是为您完成任务。请告诉我们您有什么,并问一个更具体的问题。这对我不起作用,因为我的linux找不到Array/Utils.pm。我正在使用我公司的电脑来处理这个问题。您需要从命令行“sudo cpan Array::Utils”安装Array::Utils,或者简单地从命令中复制intersect子命令,该命令说我不允许执行“/usr/bin/cpan Array::Utils”是否有其他方法来编写代码?是,将上一条注释中的链接中的sub复制到您的代码中,并删除示例中对Array::Utils的引用也不起作用。我试过Text::Diff,但从来没有像我想要的那样工作过。