in reply to getting array value from anonymous hash
#!/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;
|
|---|