in reply to Re: getting array value from anonymous hash
in thread getting array value from anonymous hash

Thanks that helped. However am confused with the following code

my @hosts = map { (keys %$_)[0] } @{ $hashref->{Inventory}{host} };
{ (keys %$_)[0] }

can be replaced simply with

 { keys %$_ }

I am not sure what

$_)[0]

actually does

Replies are listed 'Best First'.
Re^3: getting array value from anonymous hash
by Athanasius (Archbishop) on Feb 14, 2014 at 09:34 UTC

    Hello sunil9009,

    You are correct. If you know that each anonymous hash in the array contains exactly one key, then

    my @hosts = map { keys %$_ } @{ $hashref->{Inventory}{host} };

    is all you need. My concern was that the anonymous hash might be expanded to contain multiple keys. My idea was to ensure you get only the first key, by specifying the zero-subscript element of the list returned by keys %$_. But my “solution” doesn’t work, because the order of keys in a hash is unpredictable. To be on the safe side, I should have used something like:

    my @hosts = grep { /^host/ } map { keys %$_ } @{ $hashref->{Inventory} +{host} };

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,