简体   繁体   English

Perl中的相对记录分隔符

[英]Relative Record Separator in Perl

I have a data that looks like this: 我有一个看起来像这样的数据:

id:40108689 --
chr22_scrambled_bysegments:10762459:F : chr22:17852459:F (1.0),
id:40108116 --
chr22_scrambled_bysegments:25375481:F : chr22_scrambled_bysegments:25375481:F (1.0),
chr22_scrambled_bysegments:25375481:F : chr22:19380919:F (1.0),
id:1 --
chr22:21133765:F : chr22:21133765:F (0.0),

So each record is separated by id:[somenumber] -- 所以每个记录用id:[somenumber] --分隔id:[somenumber] --

What's the way to access the data so that we can have a hash of array: 访问数据的方式是什么,以便我们可以拥有数组的哈希值:

$VAR = { 'id:40108689' => [' chr22_scrambled_bysegments:10762459:F : chr22:17852459:F (1.0),'], 

         'id:40108116' => ['chr22_scrambled_bysegments:25375481:F :chr22_scrambled_bysegments:25375481:F (1.0)',
'chr22_scrambled_bysegments:25375481:F : chr22:19380919:F (1.0),'
         #...etc
       }

I tried to approach this using record separator. 我试图使用记录分隔符来解决这个问题。 But not sure how to generalize it? 但不确定如何概括呢?

{
    local $/ = " --\n";  # How to include variable content id:[number] ?

    while ($content = <INFILE>) {
      chomp $content;
      print "$content\n" if $content; # Skip empty records
    }
}
my $result = {};
my $last_id;
while (my $line = <INFILE>) {
    if ($line =~ /(id:\d+) --/) {
        $last_id = $1;
        next;
    }
    next unless $last_id; # Just in case the file doesn't start with an id line

    push @{ $result->{$last_id} }, $line;
} 

use Data::Dumper;
print Dumper $result;

Uses the normal record separator. 使用正常记录分隔符。

Uses $last_id to keep track of the last id row encountered and is set to the next id when another one is encountered. 使用$ last_id来跟踪遇到的最后一个id行,并在遇到另一个id时设置为下一个id。 Pushes non-id rows on to an array for the hash key of the last matched id line. 将非id行推送到数组,以获取最后匹配的id行的哈希键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM