我想解析文件文本然后将其放入哈希中。我的文件如下所示:
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]);
}
}
}`