Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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
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 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 :)
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)

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 taking refuge in the Monastery: (8)
As of 2014-09-20 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls