To access the anonymous array, begin by subscripting: $hashref->{Inventory}{host}. That gives you a reference to the array; you can then dereference it by putting it inside @{ ... }. So:
#! perl use strict; use warnings; use Data::Dump; my $hashref = { Inventory => { lb => 'abc', host => [ { hostA => { os => 'Linux', location => 'Dublin' + } }, { hostB => { os => 'Windows', location => 'US' + } }, { hostC => { os => 'Ubuntu', location => 'Germany +' } }, ], }, }; # Question 1 my @hosts = map { (keys %$_)[0] } @{ $hashref->{Inventory}{host} }; dd \@hosts; # Question 2 push @{ $hashref->{Inventory}{host} }, { hostD => { os => 'Unix', location => 'France' } }; dd $hashref;
Output:
0:39 >perl 871_SoPW.pl ["hostA", "hostB", "hostC"] { Inventory => { host => [ { hostA => { location => "Dublin", os => "Linux" } }, { hostB => { location => "US", os => "Windows" } }, { hostC => { location => "Germany", os => "Ubuntu" } }, { hostD => { location => "France", os => "Unix" } }, ], lb => "abc", }, } 0:39 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: getting array value from anonymous hash
by Athanasius
in thread getting array value from anonymous hash
by sunil9009
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |