Perl 中的相对记录分隔符

发布于 2025-01-01 11:06:16 字数 1014 浏览 0 评论 0原文

我有一个看起来像这样的数据:

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),

所以每条记录都由 id:[somenumber] -- 分隔

访问数据的方法是什么,以便我们可以获得数组的哈希值:

$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
       }

我尝试接近这使用记录分隔符。但不确定如何概括它?

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

    while ($content = <INFILE>) {
      chomp $content;
      print "$content\n" if $content; # Skip empty records
    }
}

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] --

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江城子 2025-01-08 11:06:17
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;

使用普通的记录分隔符。

使用 $last_id 跟踪遇到的最后一个 id 行,并在遇到另一行时设置为下一个 id。将非 id 行推送到数组中,以获取最后一个匹配的 id 行的哈希键。

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. Pushes non-id rows on to an array for the hash key of the last matched id line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文