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

Re: Converting array of hash references to hash

by hdb (Monsignor)
on Apr 22, 2013 at 09:40 UTC ( [id://1029833]=note: print w/replies, xml ) Need Help??


in reply to Converting array of hash references to hash

You can also answer your question w/o creating a hash:

$ams_ip = "" unless grep { $ams_ip eq $_->{'deviceIp'} } @{$ams_hosts} +;

but this would be computationally more expensive if you need to perform this query many times.

Replies are listed 'Best First'.
Re^2: Converting array of hash references to hash
by anirudh.agarwal (Novice) on Apr 22, 2013 at 13:33 UTC
    This one definitely works. Thanks. This query is executed only once per request in my case. I hope in that case even if  @{$ams_hosts} has a large data, it wont be expensive. :) Thanks again
Re^2: Converting array of hash references to hash
by anirudh.agarwal (Novice) on May 01, 2013 at 12:54 UTC
    I verified that use of any instead of grep increases the performance.
      > I verified that use of any instead of grep increases the performance.

      sure, but List::MoreUtils is not core...

      lanx@nc10-ubuntu:~$ corelist List::MoreUtils List::MoreUtils was not in CORE (or so I think)

      ... thats why it's easier to show working code w/o dependencies.

      With List::MoreUtils present any is for sure the better choice!

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re^2: Converting array of hash references to hash
by anirudh.agarwal (Novice) on May 01, 2013 at 09:27 UTC
    When I check the above statement using Perl Critic, it finds the following violation. "grep" used in boolean context. I see this strange.
      I then found that grep would search for the rest of the array even if a match is found in the first record itself. I believe any should be used. Let me try the following $ams_ip = "" unless any { $ams_ip eq $_->{'deviceIp'} } @{$ams_hosts}; Please let me know if I am wrong. Thanks :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found