嗨,我是perl的新手,我遇到了一个问题。我正在创建散列哈希,因为我想将多行存储为值。我的价值只是唱线。
FILE.TXT
family (){
kapoor(){
member1(){
male(){
between : 0 to 5
person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
male(){
between : 5 to 5
person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
}
member2(){
male(){
between : 0 to 5
person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
male(){
between : 5 to 5
person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
}
}
rai(){
member1(){
male(){
between : 0 to 5
person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
male(){
between : 5 to 5
person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
}
member2(){
male(){
between : 0 to 5
person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
male(){
between : 5 to 5
person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
}
}
o / p应该是:
family=>{
kapoor => {
member1=>{
0 to 5=> person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
member2=> {
5 to 5 => person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
rai =>{
/...................etc
}
我只获得单行价值,例如
family=>{
kapoor => {
member1=>{
0 to 5=> person : 2
}
member2=> {
5 to 5 => person : 20
}
rai =>{
/...................etc
}
以下是我的尝试:
while(my$line=<$fh>)
{
if($line=~/$family_name_re/)
{
$family_name=$1;
$family_count=$cell_count+1;
}
elsif ($line=~/$member_re/)
{
$member = $1;
}
elsif ($line=~/$age_re/)
{
$age_range=$1;
$data{$family_name}{$member}{$age_range}=$line;
}
}
print Dumper(\%data);
print "Number of cell is $cell_count\n";
close ($fh);
我不知道如何为value分配多行。我在分配值的同时尝试了但是没有用。
答案 0 :(得分:1)
看起来你想要%数据是HoHoHoHoH而你只有一个HoHoHoH。 例如,你想要
$data{$family_name}{$member}{$age_range}= {5 to 5 => person : 20,
their name => bla & bla bla,
bla => bla bla,
...
}
为了使这项工作,你可能想要清理你的导入代码, 并特别修复该行
$data{$family_name}{$member}{$age_range}=$line;
这只是一个HoHoHoH,这就是你输出的唯一原因 一行,而不是你想要的哈希。
所以,通过psuedo代码s / b类似
$data{$family_name}{$member}{$age_range}=%lines;
希望有所帮助。
答案 1 :(得分:0)
我找到了答案:
推送@ {$ data {$ family_name} {$ member} {$ age_range}},$ line;
:)