gpt4 book ai didi

Perl - 将文件文本解析为散列

转载 作者:行者123 更新时间:2023-12-02 08:42:14 29 4
gpt4 key购买 nike

我想解析文件文本,然后将其放入散列中。我的文件看起来像:

key1 val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,
val,val,val,val
key2 val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,
val,val,val,val
key3 val
key4 val,val
key5 val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,
val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,
val,val,val,val,val,val,val,val,val,val,val,val,val,val,val

我的键在空格之前,我的值是空格之后和每个逗号之前的元素列表。我有一些没有键的行,因为值在几行上继续。

所以我想要这样的哈希(我最熟悉 Python):

hash={'key1':[val,val,...],'key2':[val,val,...]} 

我的代码:`

my %hashNames;
open INFILE, "./file.txt" or die $!;
my @temp = ();

while (my $line = <INFILE>)
{

my @names = split /[\t,]/, $line;
my $ID = $names[0];
if ( $line =~ /\t/ )
{

my @temp=();
for (my $i = 1; $i < @names; $i +=1)
{
push (@temp, $names[$i]);
}

}
else
{

for (my $i = 0; $i < @names; $i +=1)
{
push (@temp, $names[$i]);
}
}
}`

最佳答案

您的问题是换行符不再分隔您的记录。因此,一种处理方法是禁用无效的默认输入记录分隔符 $/ 并模拟一个有效的分隔符:

use strict;
use warnings;
use Data::Dumper;

my %hash;
my $file;
{
local $/; # disable input record separator
$file = <DATA>; # entire file here now!
}

for my $line (split /^(?=\S+ )/m, $file) { # records begin this way now
$line =~ s/\n//g; # remove newlines
my ($key, $val) = split ' ', $line, 2; # divide into two fields
$hash{$key} = [ split /,/, $val ]; # store the data
}

print Dumper \%hash;

__DATA__
key1 val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,
val,val,val,val
key2 val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,
val,val,val,val
key3 val
key4 val,val
key5 val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,
val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,val,
val,val,val,val,val,val,val,val,val,val,val,val,val,val,val

解释:

  • 使用 /m 修饰符拆分 /^(?=\S+ )/m 意味着 ^ 现在将匹配字符串中的换行符,它将模拟输入记录分隔符。
  • 通过将 LIMIT 2 添加到 split
  • 来将字符串拆分为两个字段
  • 我们使用匿名数组 [ ... ] 直接拆分为散列,其中包含拆分语句。

关于Perl - 将文件文本解析为散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476540/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com