如何在perl中查找两个文件之间包含匹配项的行?

如何在perl中查找两个文件之间包含匹配项的行?,perl,Perl,我是使用perl的新手。我想做的是比较两个文件。一个是我的索引文件,我称之为“temp”。我试图使用它搜索我称之为“array”的主文件。索引文件中只有数字。我的数组中有一些行具有这些数字。我一直试图找到这两个文件之间的交集,但我的代码不起作用。这是我一直想做的 #!/usr/bin/perl print "Enter the input file:"; my $filename=<STDIN>; open (FILE, "$filename") || die "Cannot ope

我是使用perl的新手。我想做的是比较两个文件。一个是我的索引文件,我称之为“temp”。我试图使用它搜索我称之为“array”的主文件。索引文件中只有数字。我的数组中有一些行具有这些数字。我一直试图找到这两个文件之间的交集,但我的代码不起作用。这是我一直想做的

#!/usr/bin/perl
print "Enter the input file:";
my $filename=<STDIN>;
open (FILE, "$filename") || die "Cannot open file: $!";
my @array=<FILE>;
close(FILE);
print "Enter the index file:";
my $temp=<STDIN>;
open (TEMP, "$temp") || die "Cannot open file: $!";
my @temp=<TEMP>;
close(TEMP);
my %seen= ();
foreach (@array) {
    $seen{$_}=1;
 }
 my @intersection=grep($seen{$_}, @temp);
 foreach (@intersection) {
    print "$_\n";
 }
索引文件 我想把索引文件中包含一个数字的行放入一个新数组。所以使用这个例子,仅 20 CA1 MPE将被放置到一个新阵列中。
我的主文件和索引文件都比我展示的要长,但希望这能让您了解我正在尝试做什么。

我假设这样的事情

#!/usr/bin/perl
print "Enter the input file:";
my $filename=<STDIN>;
open (FILE, "$filename") || die "Cannot open file: $!";
my @array=<FILE>;
close(FILE);
print "Enter the index file:";
my $temp=<STDIN>;
open (TEMP, "$temp") || die "Cannot open file: $!";
my @temp=<TEMP>;
close(TEMP);
my %seen= ();
foreach (@array) {
    $seen{$_}=1;
 }
 my @intersection=grep($seen{$_}, @temp);
 foreach (@intersection) {
    print "$_\n";
 }
use strict;
use warnings;
use Data::Dumper;

# creating arrays instead of reading from file just for demo
# based on the assumption that your files are 1 number per line
# and no need for any particular parsing
my @array = qw/1 2 3 20 60 50 4 5 6 7/; 
my @index = qw/10 12 5 3 2/;
my @intersection = ();
my %hash1 = map{$_ => 1} @array;

foreach (@index)
{
    if (defined $hash1{$_})
    {
        push @intersection, $_;
    }
}


print Dumper(\@intersection);
退出====

$VAR1 = [
      '5',
      '3',
      '2'
    ];
有几件事:

  • 始终做到
    使用严格
    使用警告在您的程序中。这将捕获许多可能的错误
  • 读取输入后,始终
    chomp
    。Perl会自动将
    \n
    添加到读取的行末尾
    chomp
    删除
    \n
  • 学习更现代的Perl形式
  • 使用nemonic变量名<代码>$temp无法将其剪切
  • 使用空格有助于提高代码的可读性
你从来没有说过你所犯的错误。我认为这与来自主文件的输入与索引文件不匹配有关

我使用散列创建一个索引,索引文件可以通过
my($index)=split/\s+/,$line使用该索引

#! /usr/bin/env perl
#
use strict;
use warnings;
use autodie;
use feature qw(say);


print "Input file name: ";
my $input_file = <STDIN>;
chomp $input_file;             # Chomp Input!

print "Index file name: ";
my $index_file = <STDIN>;
chomp $index_file;             # Chomp Input!

open my $input_fh, "<", $input_file;

my %hash;
while ( my $line = <$input_fh> ) {
    chomp $line;
    #
    # Using split to find the item to index on
    #
    my ($index) = split /\s+/, $line;
    $hash{$index} = $line;
}
close $input_fh;

open my $index_fh, "<", $index_file;

while ( my $index = <$index_fh> ) {
    chomp $index;
    #
    # Now index can look up lines
    #
    if( exists $hash{$index} ) {
        say qq(Index: $index  Line: "$hash{$index}");
    }
    else {
        say qq(Index "$index" doesn't exist in file.);
    }
}
#/usr/bin/env perl
#
严格使用;
使用警告;
使用自动模具;
使用特征qw(例如);
打印“输入文件名:”;
我的$input_文件=;
chomp$input_file;#输入!
打印“索引文件名:”;
我的$index_文件=;
chomp$index_文件;#输入!
打开我的$input\u fh,“
!/usr/bin/perl
严格使用;
使用警告;
使用自动模具;
@ARGV='主_文件';

打开(my$fh_idx,'给出索引文件和主文件中的一行示例。发布您输入两个文件的数据和预期输出。我用请求的信息更新了主发布
#! /usr/bin/env perl
#
use strict;
use warnings;
use autodie;
use feature qw(say);


print "Input file name: ";
my $input_file = <STDIN>;
chomp $input_file;             # Chomp Input!

print "Index file name: ";
my $index_file = <STDIN>;
chomp $index_file;             # Chomp Input!

open my $input_fh, "<", $input_file;

my %hash;
while ( my $line = <$input_fh> ) {
    chomp $line;
    #
    # Using split to find the item to index on
    #
    my ($index) = split /\s+/, $line;
    $hash{$index} = $line;
}
close $input_fh;

open my $index_fh, "<", $index_file;

while ( my $index = <$index_fh> ) {
    chomp $index;
    #
    # Now index can look up lines
    #
    if( exists $hash{$index} ) {
        say qq(Index: $index  Line: "$hash{$index}");
    }
    else {
        say qq(Index "$index" doesn't exist in file.);
    }
}
#!/usr/bin/perl
use strict;
use warnings;
use autodie;

@ARGV = 'main_file';

open(my $fh_idx, '<', 'index_file');
chomp(my @idx = <$fh_idx>);
close($fh_idx);

while (defined(my $r = <>)) {
    print $r if grep { $r =~ /^[ \t]*$_/ } @idx;
}