Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Converting array of hash references to hash

by hdb (Prior)
on Apr 22, 2013 at 09:30 UTC ( #1029831=note: print w/ replies, xml ) Need Help??


in reply to Converting array of hash references to hash

In map you need to return pairs to fill your hash:

my %ams_ips_hash = map { $_->{'deviceIp'} => 1 } @{$ams_hosts};


Comment on Re: Converting array of hash references to hash
Download Code
Re^2: Converting array of hash references to hash
by tobyink (Abbot) on Apr 22, 2013 at 09:51 UTC

    Personally I'd...

    my %ams_ips_hash = map { $_->{'deviceIp'} => $_ } @{$ams_hosts};
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Thanks for your reply. This one stores (key,value) as (IP, entire hash). This is again not what I want.
Re^2: Converting array of hash references to hash
by anirudh.agarwal (Novice) on Apr 22, 2013 at 13:14 UTC
    What actually happens in your case is, the value of deviceIp, say, 172.16.133.56 changes to 172.16 . Is it because that perl is taking it as an integer?

      I do not understand. If I run:

      use strict; use warnings; use Data::Dumper; my $ams_hosts = [ { 'domainId' => '1', 'deviceIp' => '172.16.133.56', 'deviceType' => 3, 'deviceName' => 'AMS' }, { 'domainId' => '1', 'deviceIp' => '172.16.133.57', 'deviceType' => 3, 'deviceName' => 'ams_57' } ]; my %ams_ips_hash = map { $_->{'deviceIp'} => 1 } @{$ams_hosts}; print Dumper( \%ams_ips_hash );

      I get

      $VAR1 = { '172.16.133.56' => 1, '172.16.133.57' => 1 };

      Can you clarify what you mean please?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-12-22 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (121 votes), past polls