Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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};

Replies are listed 'Best First'.
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, 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' => '', 'deviceType' => 3, 'deviceName' => 'AMS' }, { 'domainId' => '1', 'deviceIp' => '', 'deviceType' => 3, 'deviceName' => 'ams_57' } ]; my %ams_ips_hash = map { $_->{'deviceIp'} => 1 } @{$ams_hosts}; print Dumper( \%ams_ips_hash );

      I get

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

      Can you clarify what you mean please?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029831]
[ambrus]: Corion: ah, so you want a library that parses HTTP, and you want to do the IO yourself, and don't want a full AnyEvent wrapper.
[ambrus]: Corion: I think I parsed a HTTP header from a string with LWP once. You can definitely use that to create a HTTP message too. The problme is
[ambrus]: that if you do that, you'd have to find where each HTTP response ends, which is nontrivial if you want persistent connections (essential for performance if you have small requests).
[Corion]: ambrus: Yes, ideally an API that I feed the incoming data piece by piece and that I can ask "is that response done" and "what should I do next" and "please construct the appropriate redirect for me"
[Corion]: ambrus: Yes, ideally the module would do all that nasty stuff for me and give me a way to ask it what the current situation is

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2016-12-07 15:59 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (130 votes). Check out past polls.