Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Converting array of hash references to hash

by hdb (Prior)
on Apr 22, 2013 at 09:40 UTC ( #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.


Comment on Re: Converting array of hash references to hash
Download Code
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
Node Status?
node history
Node Type: note [id://1029833]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2015-07-29 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls