我从这样的文本文件创建了一个哈希表:
use strict;
use warnings;
my %h;
open my $fh, '<', 'tst' or die "failed open 'tst' $!";
while ( <$fh> ) {
push @{$h{keys}}, (split /\t/)[0];
}
close $fh;
use Data::Dumper;
print Dumper \%h;
现在我想在哈希表的另一个文本文件中查找一个字段。如果存在,则将当前行写入结果文件:
use strict;
use warnings;
my %h;
open my $fh, '<', 'tst' or die "failed open 'tst' $!";
while ( <$fh> ) {
push @{$h{keys}}, (split /\t/)[0];
}
close $fh;
use Data::Dumper;
print Dumper \%h;
open (my $fh1,"<", "exp") or die "Can't open the file: ";
while (my $line =<$fh1>){
chomp ($line);
my ($var)=split(">", $line);
if exists $h{$var};
print ($line);
}
我得到了这些错误:
syntax error at codeperl.pl line 26, near "if exists"
Global symbol "$line" requires explicit package name at codeperl.pl line 27.
syntax error at codeperl.pl line 29, near "}"
Execution of codeperl.pl aborted due to compilation errors.
请问有什么想法吗?