Reputation: 2697
I am trying to filter values in an array using grep (in Perl). My goal is to remove array elements where both the "pending" element is INVALID_APNS and the "token" element is empty
This does not work - it deletes everything. The array contains 0 elements after the rep, but should contain 2.
use constant INVALID_APNS => -1;
@active_connections = (
{
pending => INVALID_APNS,
token=>'123'
},
{
pending => INVALID_APNS,
token=>'123'
},
{
pending => INVALID_APNS,
token=>''
},
);
@active_connections = grep { ($_->{pending} != INVALID_APNS) && ($_->{token} ne '')} @active_connections;
print scalar @active_connections;
What am I doing wrong?
Upvotes: 1
Views: 837
Reputation: 98508
If you want to exclude records with $_->{'pending} == INVALID_APNS && $_->{'token'} eq ''
, you are negating that improperly to get what grep should include. Either of these should do what you want:
! ( $_->{'pending'} == INVALID_APNS && $_->{'token'} eq '' )
or
$_->{'pending'} != INVALID_APNS || $_->{'token'} ne ''
See https://en.wikipedia.org/wiki/De_Morgan%27s_laws
Upvotes: 3