Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
正确使用Perl“;“存在”;_Perl_Hash - Fatal编程技术网

正确使用Perl“;“存在”;

正确使用Perl“;“存在”;,perl,hash,Perl,Hash,我有两个文件。前两列都是染色体位点和基因型,例如chr1:1736464585和T/G 我已将前两列放入哈希中。我想检查第二个文件中是否存在哈希键(染色体轨迹) 我编写了这个Perl程序,并尝试了许多变体,但我不确定是否正确使用了exists:它给出了错误exists不是散列或数组元素或子例程 #!/usr/bin/perl use strict; use warnings; my $output = "annotated.txt"; open( O, ">>$output" )

我有两个文件。前两列都是染色体位点和基因型,例如
chr1:1736464585
T/G

我已将前两列放入哈希中。我想检查第二个文件中是否存在哈希键(染色体轨迹)

我编写了这个Perl程序,并尝试了许多变体,但我不确定是否正确使用了
exists
:它给出了错误
exists不是散列或数组元素或子例程

#!/usr/bin/perl

use strict;
use warnings;

my $output = "annotated.txt";
open( O, ">>$output" );

my $filename  = "datatest.txt";
my $filename2 = "MP2.txt";

chomp $filename;
chomp $filename2;

my %hash1 = ();

open( FN1, $filename ) or die "Can't open $filename: $!";
my @lines = <FN1>;

foreach my $line (@lines) {

    my @split = split /\t/, $line;

    if ( $line =~ /^chr/ ) {

        my ( $key, $value ) = ( $split[0], $split[1] );
        $hash1{$key} = $value;
    }
}

my $DATA;
open( $DATA, $filename2 ) or die $!;
my @lines2 = <$DATA>;

foreach my $line2 (@lines2) {

    my @split2 = split /\t/, $line2;

    if ( $line2 =~ /^chr/ ) {

        if ( exists %hash1{$key} ) {

            print "$line2\n";
        }
    }
}
#/usr/bin/perl
严格使用;
使用警告;
my$output=“annotated.txt”;
打开(O,“>>$output”);
my$filename=“datatest.txt”;
my$filename2=“MP2.txt”;
chomp$filename;
chomp$filename2;
我的%hash1=();
打开(FN1,$filename)或死亡“无法打开$filename:$!”;
我的@lines=;
foreach my$行(@行){
my@split=split/\t/,$line;
如果($line=~/^chr/){
我的($key,$value)=($split[0],$split[1]);
$hash1{$key}=$value;
}
}
我的$数据;
打开($DATA,$filename2)或死亡$!;
我的@lines2=;
foreach my$line2(@lines2){
my@split2=split/\t/,$line2;
如果($line2=~/^chr/){
if(存在%hash1{$key}){
打印“$line2\n”;
}
}
}

以下行的语法不正确:

if (exists %hash1{$key}) { ... }
这应该是:

if (exists $hash1{$key}) { ... }

以下行的语法不正确:

if (exists %hash1{$key}) { ... }
这应该是:

if (exists $hash1{$key}) { ... }

?空白-缩进和空行-是免费的,将使您的软件更方便可视化和调试。如果您必须请求他人帮助,那么尽可能使代码可读才是礼貌之举。在本例中,我为您完成了此操作。您必须指定标量$hash{key}上存在感谢Borodin,抱歉-这是我第一次发布,因此不确定此礼仪?空白-缩进和空行-是免费的,将使您的软件更方便可视化和调试。如果您必须请求他人帮助,那么尽可能使代码可读才是礼貌之举。在这个例子中,我已经为你做了。你必须指定存在于标量$hash{key}上。谢谢Borodin,抱歉-这是我第一次发布,所以我不确定这种礼节。谢谢Bjerre和Abhay!谢谢Bjerre和Abhay!