sunil9009 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $hashref = { 'Inventory' => { 'lb' => "abc", 'host' => [{ 'hostA' => { 'os' => "Linux",'locat +ion'=>"Dublin"}}, {'hostB' => {'os'=> "Windows",'locat +ion'=>"US"}}, {'hostC' => {'os'=>"Ubuntu",'locatio +n'=>"Germany"}} ] } } ;

Question 1: I just want the array values for names of the host @hosts = ('hostA','hostB','hostC');

Question 2: How can I push a new array value like push @hosts,$newvalue{'os'=>'Unix','location'=>'France'}

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

    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,

      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

        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,

Re: getting array value from anonymous hash
by karlgoethebier (Abbot) on Feb 13, 2014 at 14:48 UTC

    IHMO your inventory has hosts that have a name, an OS and a location, isn't it? Please see also perldsc about handling complexer data structures, populating and accessing them and so on.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: getting array value from anonymous hash
by simmisam (Novice) on Feb 13, 2014 at 22:56 UTC
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @hosts; my %arrayValue; my $hashref = { 'Inventory' => { 'lb' => "abc", 'host' => [{ 'hostA' => { 'os' => "Linux",'locat +ion'=>"Dublin"}}, {'hostB' => {'os'=> "Windows",'locat +ion'=>"US"}}, {'hostC' => {'os'=>"Ubuntu",'locatio +n'=>"Germany"}} ] } } ; # Question 1: I just want the array values for names of the host @host +s = ('hostA','hostB','hostC'); my $x = ${$hashref}{'Inventory'}{'host'}; foreach my $hash (@{$x}) { foreach(keys $hash) { push (@hosts,$_); } } print @hosts; # Question 2: How can I push a new array value like push @hosts,$newva +lue{'os'=>'Unix','location'=>'France'} $arrayValue{'hostD'} = {'os'=>'Unix','location'=>'France'}; push(@{$x},\%arrayValue); print Dumper $hashref;