N
niall.macpherson
I have a very deep nested hash and I need to set a lot of values at one
of the lowest levels.
Eg if I wanted to do something like
$a{b}{c}{d}{e}{f}{'bit1} = 'val1';
$a{b}{c}{d}{e}{f}{'bit2} = 'val2';
......... etc etc
I'd like to be able to assign a variable (say $thisone) to the top
levels ($a{b}{c}{d}{e}{f}) so that I could do
$thisone->{'bit1} = 'val1';
$thisone->{'bit2} = 'val2';
Which would make it more readable.
I have partly achieved this - the following code works
------------------------------------------------------------------------------------------------------------------------------
use strict;
use warnings;
use Data:umper;
my %tableinfo;
while(<DATA>)
{
chomp;
my($table, $tstamp, $value, $morestuff, $yetmore, $evenmore) =
split(/:/);
$tableinfo{$table}{total} += $value;
## Want to make this line more readable
$tableinfo{$table}{stats}{$tstamp}{value} = $value; ## simplify this
line
my $thisone = $tableinfo{$table}{stats}{$tstamp}; ## assign
shortcut
## Can't do $thisone->{value} = $value . Autovivification ?
$thisone->{morestuff} = $morestuff;
$thisone->{yetmore} = $yetmore;
$thisone->{evenmore} = $evenmore;
$tableinfo{$table}{total} += $value;
}
print Dumper \%tableinfo;
__DATA__
firstone:12345:11111:9999:ccc:vvv
secondone:16767:22222:8888:bbb:nnn
firstone:12367:33333:7777:zzz:qqq
firstone:12389:44444:6666:www:xxx
secondone:17878:555555:4444:dddpp
thirdone:14545:66666:1111:qqq:lll
thirdone:15656:7777:aaaaa:aaaaa:zzzz
-------------------------------------------------------------------------------------------------------------------------------
However as you can see I have been unable to shorten the first
assignnment line (marked 'simplify this line').
I can't do the line marked 'assign shortcut' and then $thisone->{value}
= $value presumably because until $tableinfo{$table}{stats}{$tstamp}
has been autovivified in the first assignment it doesn't exist.
My real world structure has about 8 levels of nexting so I want to keep
it as readable as possible.
Any suggestions for the best way to do this ?
Thanks
of the lowest levels.
Eg if I wanted to do something like
$a{b}{c}{d}{e}{f}{'bit1} = 'val1';
$a{b}{c}{d}{e}{f}{'bit2} = 'val2';
......... etc etc
I'd like to be able to assign a variable (say $thisone) to the top
levels ($a{b}{c}{d}{e}{f}) so that I could do
$thisone->{'bit1} = 'val1';
$thisone->{'bit2} = 'val2';
Which would make it more readable.
I have partly achieved this - the following code works
------------------------------------------------------------------------------------------------------------------------------
use strict;
use warnings;
use Data:umper;
my %tableinfo;
while(<DATA>)
{
chomp;
my($table, $tstamp, $value, $morestuff, $yetmore, $evenmore) =
split(/:/);
$tableinfo{$table}{total} += $value;
## Want to make this line more readable
$tableinfo{$table}{stats}{$tstamp}{value} = $value; ## simplify this
line
my $thisone = $tableinfo{$table}{stats}{$tstamp}; ## assign
shortcut
## Can't do $thisone->{value} = $value . Autovivification ?
$thisone->{morestuff} = $morestuff;
$thisone->{yetmore} = $yetmore;
$thisone->{evenmore} = $evenmore;
$tableinfo{$table}{total} += $value;
}
print Dumper \%tableinfo;
__DATA__
firstone:12345:11111:9999:ccc:vvv
secondone:16767:22222:8888:bbb:nnn
firstone:12367:33333:7777:zzz:qqq
firstone:12389:44444:6666:www:xxx
secondone:17878:555555:4444:dddpp
thirdone:14545:66666:1111:qqq:lll
thirdone:15656:7777:aaaaa:aaaaa:zzzz
-------------------------------------------------------------------------------------------------------------------------------
However as you can see I have been unable to shorten the first
assignnment line (marked 'simplify this line').
I can't do the line marked 'assign shortcut' and then $thisone->{value}
= $value presumably because until $tableinfo{$table}{stats}{$tstamp}
has been autovivified in the first assignment it doesn't exist.
My real world structure has about 8 levels of nexting so I want to keep
it as readable as possible.
Any suggestions for the best way to do this ?
Thanks