naf
naf

Reputation: 95

perl expression && and eq

I have a Perl script where I can add a prefilter.

if ($row->{'category'}  eq 'Software') { return undef; }

this works as expected.

But if I do this

if ($row->{'category'}  eq 'Software' &&  $row->{'title'} =~ m/EDU|OLP|Molp|MOLP|Media/) { return undef; }

I can't get it to work. I don't get any errors.

I spent the last hour on line looking for a solution but I can't seem to find it.

I have also tried and instead of &&.

This is the Data::Dumper output of $row

$VAR1 = {
      'manufacturer_model' => 'FQC-02310           ',
      'junk' => '42.03',
      'manufacturer' => 'Microsoft                     ',
      'model' => 'MSWIN81     ',
      'price' => '28.02',
      'title' => 'Win Pro SA EDU NL                       ',
      'category' => 'Software            ',
      'stock' => '251'
    };

Upvotes: 0

Views: 134

Answers (4)

whoopes
whoopes

Reputation: 144

If you provided sample data of what you'd expect to match and not match, that would help.

EDIT
Based on the sample data, I'd change your regex to:

$row->{'title'} =~ /(EDU|OLP|Molp|MOLP|Media)/i

Upvotes: -1

Borodin
Borodin

Reputation: 126722

The problem is that your hash fields have trailing spaces after the text values. If you are building the hash properly, for instance using Text::CSV, then you must process the data you have collected using

s/\s+\z// for values %$row

before interrogating it. But if the spurious whitespace is a result of incorrect decoding of CSV data then you should fix that instead.

Upvotes: 3

aidan
aidan

Reputation: 9576

The problem is here:

$row->{'category'}  eq 'Software'

Because $row->{'category'} is actually

'Software            '

With that in mind, this should work:

$row->{'category'} =~ /^Software\s*$/

Upvotes: 4

Jeff Ferland
Jeff Ferland

Reputation: 18282

$row->{'category'} = 'Software';
$row->{'title'} = 'Blank Media';
if (
    $row->{'category'}  eq 'Software'
    &&  $row->{'title'} =~ m/EDU|OLP|Molp|MOLP|Media/
) { print "OK!\n"; }

That prints "OK!" It looks to me like your data isn't what you expected it to be (you have a problem in $row->{'title'}). Check your source data, check your parser.

Upvotes: 0

Related Questions