2

我有一个制表符分隔的文件:abc.txt。其中有如下数据:

Pytul_T015270   Protein of unknown function
Pytul_T015269   Protein of unknown function
Pytul_T015255   Protein of unknown function
Pytul_T015297   Protein of unknown function

我正在创建一个解析器,它将此 abc.txt 和其他 2 个文件作为输入,并通过从包中调用不同的子例程来解析文件:utility.pm

解析的子程序abc.txt在我的包中定义,utility.pm如下:

use strict;

sub readblast{

my $fileName = shift;
my %hash;
my %geneNameHash;

open PRED, $fileName or die "Can't open file $!\n";
while (my $line=<PRED>) {

    chomp $line;
    #print $line,"\n";
    (my $gene,my $desc) =  split /\t/, $line;

    $hash{$gene} = $desc;
}

close(PRED);

return %hash;
}

而我的使用哈希的 parser.pl 脚本如下:

my %blast=&utility::readblast($ARGV[2]);
for my $mRNA(keys %{ $featureHash{$scaffold}{$gene}}){
my $desc = $blast{$mRNA};
}

$featurehash是我从另一个文件制作的另一个哈希。并$mRNA具有文件的键值abc.txt

但是 $desc 的输出是空白的,我收到错误:

Use of uninitialized value $desc in concatenation (.) or string at parser.pl

有什么问题my $desc = $blast{$mRNA};为什么它不存储 abc.txt 的第二列?

4

1 回答 1

4

以下内容可防止尾随空行和可能的非制表符分隔符(通过使用split限制):

#!/usr/bin/env perl

package My::Utility;

use strict;
use warnings;

sub read_blast {
    my $fh = shift;

    my %hash;

    while (my $line = <$fh>) {
        chomp $line;
        last unless $line =~ /\S/;
        my ($key, $value) = split ' ', $line, 2;
        $hash{ $key } = $value;
    }

    return \%hash;
}

package main;

my $blast = My::Utility::read_blast(\*DATA);
while (my ($k, $v) = each %$blast) {
    print "'$k' => '$v'\n";
}

__DATA__
Pytul_T015270   Protein of unknown function
Pytul_T015269   Protein of unknown function
Pytul_T015255   Protein of unknown function
Pytul_T015297   Protein of unknown function
于 2013-08-23T14:46:37.083 回答