Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Strange bevahiour of grep function.

by kmxillo (Initiate)
on May 14, 2013 at 15:33 UTC ( #1033508=perlquestion: print w/ replies, xml ) Need Help??
kmxillo has asked for the wisdom of the Perl Monks concerning the following question:

I have two arrays of hashes (@r_objects and @r_objects). A want to find the value in @A that differs from @B. Using grep function with the condition 'eq' works fine. My problem is when I use 'ne', it shows all $_obj_prevVal from @prev_values. Here is my code.

my @_remainObj = map { my $_obj_prevVal = $_->{OBJECT}; my $_mnum_prevVal = $_->{METRIC}; my $_notObj_rObjects = grep { $_obj_prevVal ne $_->{OBJECT} and +$_mnum_prevVal eq "1"} @r_objects; $_notObj_rObjects ? $_obj_prevVal : (); } @prev_values;
Does anyone can help me? Thank you

Comment on Strange bevahiour of grep function.
Download Code
Re: Strange bevahiour of grep function.
by LanX (Canon) on May 14, 2013 at 15:57 UTC
    Hard to read, but if you want to invert a logical clause you also have to switch and to or and invert all sub-clauses.

    see De_Morgan's_laws

    And consider to use List::MoreUtils 'any' instead of grep.

    HTH =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    update

    s/ List::Util / List::MoreUtils /

Re: Strange bevahiour of grep function.
by RichardK (Priest) on May 14, 2013 at 16:55 UTC

    You don't need the test of $_mnum_prevVal in the grep as it never changes (it doesn't refer to $_), so you can move that outside the grep and that might make things easier.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-07-13 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (247 votes), past polls