如何读取数据块并将其解析为数组的Perl哈希值?

时间:2010-04-16 06:19:54

标签: perl data-structures

我的数据如下:

#info
#info2

1:SRX004541
Submitter: UT-MGS, UT-MGS
Study: Glossina morsitans transcript sequencing project(SRP000741)
Sample: Glossina morsitans(SRS002835)
Instrument: Illumina Genome Analyzer
Total: 1 run, 8.3M spots, 299.9M bases
Run #1: SRR016086, 8330172 spots, 299886192 bases

2:SRX004540
Submitter: UT-MGS
Study: Anopheles stephensi transcript sequencing project(SRP000747)
Sample: Anopheles stephensi(SRS002864)
Instrument: Solexa 1G Genome Analyzer
Total: 1 run, 8.4M spots, 401M bases
Run #1: SRR017875, 8354743 spots, 401027664 bases

3:SRX002521
Submitter: UT-MGS
Study: Massive transcriptional start site mapping of human cells under hypoxic conditions.(SRP000403)
Sample: Human DLD-1  tissue culture cell line(SRS001843)
Instrument: Solexa 1G Genome Analyzer
Total: 6 runs, 27.1M spots, 977M bases
Run #1: SRR013356, 4801519 spots, 172854684 bases
Run #2: SRR013357, 3603355 spots, 129720780 bases
Run #3: SRR013358, 3459692 spots, 124548912 bases
Run #4: SRR013360, 5219342 spots, 187896312 bases
Run #5: SRR013361, 5140152 spots, 185045472 bases
Run #6: SRR013370, 4916054 spots, 176977944 bases

我想要做的是创建一个数组的哈希值,每个块的第一行作为键 和SR ##部分行以“^ Run”作为其数组成员:

$VAR = {
     'SRX004541' => ['SRR016086'], 
     # etc
}

但为什么我的构造不起作用。它必须是一种更好的方法。

use Data::Dumper;
my %bighash;
my $head = "";
my @temp = ();

while ( <> ) {
    chomp;
    next if (/^\#/);


    if ( /^\d{1,2}:(\w+)/ ) { 
print "$1\n";
      $head = $1;


    }
    elsif (/^Run \#\d+: (\w+),.*/){ 
print "\t$1\n";
      push @temp, $1;
    }
    elsif (/^$/) {
         push @{$bighash{$head}}, [@temp];
         @temp =();
    }

}               


print Dumper \%bighash ;

5 个答案:

答案 0 :(得分:5)

像这样解析的另一种方法是阅读整个段落。有关输入记录分隔符($/)的详细信息,请参阅perlvar

例如:

use strict;
use warnings;
use Data::Dumper qw(Dumper);
my %bighash;

{
    local $/ = "\n\n"; # Read entire paragraphs.
    while (my $paragraph = <>){
        # Filter out comments and handle extra blank lines between sections.
        my @lines = grep {/\S/ and not /^\#/} split /\n/, $paragraph;
        next unless @lines;

        # Extract the key and the SRR* items.
        my $key = $lines[0];
        $key =~ s/^\d+://;
        $bighash{$key} = [map { /^Run \#\d+: +(SRR\d+)/ ? $1 : () } @lines];
    }
}

print Dumper(\%bighash);

答案 1 :(得分:2)

替换

push @{$bighash{$head}}, [@temp];

push @{$bighash{$head}}, @temp;

每个$head值只有一个数组,对吗?第二个语句将@temp中的所有值添加到$bighash{$head}中的arrayref。另一方面,第一个表单从@temp中的项目构建数组引用,并将 推送到$bighash{$head},给您一个arrayrefs的arrayref。

或者你可能想要

$bighash{$head} = [@temp];

如果您只希望遇到一次$head值。

答案 2 :(得分:1)

根据您的代码,这是一种方法

my $head;
my %result;
while (<>) {
    chomp;
    next if (/^\#/);

    if ( /^\d{1,2}:(\w+)/ ) {
        $result{$1} = []; 
        $head = $1; # $head will be used to know which key the following values
                    # will be assigned to
    }
    elsif (/^Run \#\d+: (\w+),.*/) {
        push(@{$result{$head}},$1); #Add the number found to the array that is assigned to the                        
                                    #last key found
    } 
}

答案 3 :(得分:0)

状态机出现问题,我想你可以使用这个逻辑:

if(!$head)
{
  # seek and get head
} 
else
{
  if (!$total) 
  {
    # seek and get total
  }
  else
  {
    # seek run
    # if found :
      # push run to temp and decrease total
      # if total eq 0 :
        # push temp to bighash
        # reset head, total and temp
  }
}

答案 4 :(得分:0)

代码看起来正确但我强烈建议添加:

use warnings
use strict

除了最琐碎的一个衬里外,还要添加

 elsif ($head && /^$/) {

到你的最后一个条件,赶上问题。