Nested Perl Hash -
i'm trying take value hash , use key next level hash, code works, thinking there has better way this.
is there way avoid k* array variables?
my @key = keys %data3; $devicetype = $key[0]; @k = keys %{$data3 {$devicetype} }; ## uber ugly, find better way. $measinfoid = $k[0]; @k1 = keys %{$data3 {$devicetype} {$measinfoid}}; $deviceid = $k1[0]; @k2 = keys %{$data3 {$devicetype} {$measinfoid} {$deviceid}}; $location = $k2[0]; @k3 = %{$data3 {$devicetype} {$measinfoid} {$deviceid} {$location}}; $measobjectldn = $k3[0]; print ("data: $devicetype, $measinfoid, $deviceid, $location, $measobjectldn\n"); foreach $m ( keys %{ $data3 {$devicetype} {$measinfoid} {$deviceid} {$location} {$measobjectldn} } ){ print("ok: $m\n"); }
%data3
looks this:
$var1 = 'nthlrfe'; $var2 = { 'dia' => { 'njbb-hlr-2' => { 'njbb' => { 'lte-1/dia-1' => { 'diaunknownavps' => '0', 'diacerrejsysinovl' => '0', 'diaprotocolerrors' => '0', 'diawrongprottype' => '0', 'diamessagercvd' => '0', 'diaoctetsent' => '0', 'diacerrejprntinwhtlst' => '0', 'diaoctetrcvd' => '0', 'diamessagedscrd' => '0', 'diacerrejconavailable' => '0', 'diamessagesent' => '0', 'diacerrejmaxconexcd' => '0' } } } } };
thanks!
keys aren't sorted, "first key" won't same.
that said, if want compact bit, following should work:
my $devicetype = (keys %data3)[0]; .... $measinfoid = (keys $data3{$devicetype})[0]; .... $deviceid = (keys $data3{$devicetype}{$measinfoid})[0];
this ugly , not want guy comes after reading this.
keys work on hash refs hashes starting perl 5.14. if you're older that, you'll have dereference full on hash.
Comments
Post a Comment