Perl accessing a hash within a hash and looping through each to extract the value
I am trying to access a hash within a hash and loop through it to get the values. here is an example of the data
$VAR1 = {
'24.40.53.143' => {
'ServStat' => {
'1.18.118.115.95.99.119.98.98.112.109.45.97.112.95.104.116.116.112.115' => 'vs_cgggbpm-ap_https',
'1.17.118.115.95.99.119.98.115.102.97.45.97.112.95.104.116.116.112' => 'vs_cddedsfa-ap_http',
'20.18.118.115.95.99.119.98.116.119.98.45.98.112.95.104.116.116.112.115' => '0',
'19.17.118.115.95.99.119.98.119.115.45.97.112.95.104.116.116.112.115' => '0',
'2.18.118.115.95.99.119.98.116.119.98.45.98.112.95.104.116.116.112.115' => '0',
'24.18.118.115.95.99.119.98.116.119.98.45.97.112.95.104.116.116.112.115' => '0',
开发者_开发技巧 '17.17.118.115.95.99.119.98.119.98.45.97.112.95.104.116.116.112.115' => '0',
'29.17.118.115.95.99.119.98.116.119.112.45.98.112.95.104.116.116.112' => '0',
I would like to loop through 'ServStat' and extract each values. How would I reference the hash 'ServStat' so that I can do a foreach on the contents? Something like this:
foreach {ServStat} {
my ( $num, $char, $vs ) = (/(\d+)\.(\d+)\.(.+)/ );
if ($num == 1) {
print {ServStat}->$value
}
}
Thank you in advance for any advise you can offer!
To get the keys, you can use the function keys on the hash.
my $data = {
'24.40.53.143' => {
'ServStat' => {'1.18.118.115.95.99.119.98.98.112.109.45.97.112.95.104.116.116.112.115' => 'vs_cgggbpm-ap_https'}
}
};
my $ServStat = $data->{24.40.53.143}{ServStat};
foreach my $key (keys %{$ServStat}) { # you need the {} to dereference as $ServStat is a hash reference
...#Now, in $key, you have the key 1.18.118.115.95.99.119.98.98.112.109.45.97.112.95.104.116.116.112.115
}
If you just want all values, just use the function values on the hash
my @values = values %{$ServStat};
精彩评论