Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: getting array value from anonymous hash

by simmisam (Novice)
on Feb 13, 2014 at 22:56 UTC ( [id://1074911]=note: print w/replies, xml ) Need Help??


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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1074911]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2026-03-06 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.