Perl-如何将数组值与哈希表进行比较

Perl-如何将数组值与哈希表进行比较,perl,Perl,新的Perl用户 我试图从存储在变量$blast\u results中的一些制表符分隔的数据创建一个哈希表,第一列作为键。然后我想将数组@filenames中的值与哈希表中的键进行比较。如果在散列键中找到数组值,我想打印出$blast_results的重新排序结构,如果数组值不在散列中,我想打印出后跟“No Result found”的值 这就是我到目前为止所知道的,我认为哈希表是不正确的: #!/usr/bin/env perl use strict; use warnings; use D

新的Perl用户

我试图从存储在变量
$blast\u results
中的一些制表符分隔的数据创建一个哈希表,第一列作为键。然后我想将数组
@filenames
中的值与哈希表中的键进行比较。如果在散列键中找到数组值,我想打印出
$blast_results
的重新排序结构,如果数组值不在散列中,我想打印出后跟“No Result found”的值

这就是我到目前为止所知道的,我认为哈希表是不正确的:

#!/usr/bin/env perl

use strict;
use warnings;
use Data:Dumper;

#create variable to mimic blast results
my $blast_results = "file1.ab1  9   350 0.0 449 418 418 403479  403042  567
file3.ab1   2   833 0.0 895 877 877 3717226 3718105 984";

#create array to mimic filename array
my @filenames = ("file1.ab1", "file2.ab1", "file3.ab1");

#header for file
my $header = "Query\tSeq_length\tTarget found\tScore (Bits)\tExpect(E-value)\tAlign-length\tIdentities\tPositives\tChr\tStart\tEnd\n";

#initialize hash
my %hash;
#split blast results into array
my @row = split(/\s+/, $blast_results);
$hash{$row[0]}=$_;
print $header;
foreach my $file (@filenames){
    ## If this filename has an associated entry in the hash, print it in a re-ordered format
    if(defined($hash{$file})){
        print "$row[0]\t$row[9]\t$row[1]:$row[7]-$row[8]\t$row[2]\t$row[3]\t$row[4]\t$row[5]\t$row[6]\t$row[1]\t$row[7]\t$row[8]\n";
        }
    ## If not, print this.
    else{
        print "$file\t0\tNo Blast Results: Sequencing Rxn Failed\n";
        }
    }
print "-----------------------------------\n";      
print "$blast_results\n"; #test what results look like
print "-----------------------------------\n"; 
print "$row[0]\t$row[1]\n"; #test if array is getting split correctly
print "-----------------------------------\n"; 
print "$filenames[2]\n"; #test if other array present
print "-----------------------------------\n";
print Dumper(\%hash);  #print out hash table
此脚本的结果是(
@filenames
数组与哈希不匹配,并且哈希不包含所有数据):


你在这里做了一件很奇怪的事:

my %hash;
#split blast results into array
my @row = split(/\s+/, $blast_results);
$hash{$row[0]}=$_;
你的散列将有一个单一的键“file1.ab1”和。。。我看不出此时会发生什么,但几乎可以肯定这就是你想要的。(从末尾的转储程序输出来看,它是
undef

在任何情况下,你都不会在这个散列中放入任何其他内容,这就是为什么你会得到你所做的结果

是否正常从文件中读取
blast\u结果

如果是这样的话,类似的方法可能会奏效:

use strict;
use warnings;
use Data::Dumper;
my %hash;

while (<DATA>) {
    my @line     = split;
    my $filename = shift(@line);
    $hash{$filename} = join( " ", @line );
}

print Dumper \%hash;


__DATA__
file1.ab1  9   350 0.0 449 418 418 403479  403042  567
file3.ab1   2   833 0.0 895 877 877 3717226 3718105 984
使用严格;
使用警告;
使用数据::转储程序;
我的%hash;
而(){
我的@line=split;
我的$filename=shift(@line);
$hash{$filename}=join(“,@line);
}
打印转储程序\%hash;
__资料__
文件1.ab1 9 350 0.0 449 418 418 403479 403042 567
文件3.ab1 2 833 0.0 895 877 877 3717226 3718105 984
如果您从命令中获得结果,为什么不试试:

open ( my $blast_results, "|-", "blastn -query test.fa -outfmt 6" );
while ( <$blast_results> ) { 
    #parse line into hash
}
open(我的$blast_结果,“|-”,“blastn-query test.fa-outfmt 6”);
而{
#将行解析为哈希
}

是的,我试图修改从文件加载哈希的内容。但我认为在最后只写文件可能更好。真正的脚本运行这样的命令行函数
my$blast\u result=blastn-query test.fa-outfmt 6
所以我认为可以在变量中使用该输出创建一个散列,然后按照我想要的方式处理文本文件。OK。可能是
open
的作业,因此您可以逐行进行。请参阅编辑。因此,我正在运行带有反勾号的命令行程序,但改用open将允许我逐行读取哈希表?是的。像这样打开打开一个文件句柄,就像STDIN一样,您可以一次读取一行。
open ( my $blast_results, "|-", "blastn -query test.fa -outfmt 6" );
while ( <$blast_results> ) { 
    #parse line into hash
}