Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Problem when comparing strings as opposed to numerical values

by ig (Vicar)
on Jul 19, 2012 at 20:50 UTC ( #982715=note: print w/ replies, xml ) Need Help??


in reply to Problem when comparing strings as opposed to numerical values

I wonder if you are missing quotes in your second if.

I'm guessing that you also need to put quotes around your strings in what I'm guessing you want to be a string eval (the second if). The string you want to evaluate should probably be something like "'string' eq 'string'".

It might help you to examine what you are evaluating. You might use the debugger but I would probably use print or warn myself. Something like:

if (eval "$hash{$filter[0]->[0]} $filter[0]->[1] $filter[0]->[ +2]") { print "eval \"$hash{$filter[1]->[0]} $filter[1]->[1] $filt +er[1]->[2]\""; if (eval "$hash{$filter[1]->[0]} $filter[1]->[1] $filter[1 +]->[2]") { if (eval "$hash{$filter[2]->[0]} $filter[2]->[1] $filt +er[2]->[2]") { print OUTFILE $line, "\n"; } } }

When you see what you are evaluating, the problem will likely become evident.

FWIW: I don't think I would use eval as you are trying to. I would probably do something more like the follwing:

sub match { my ($hash, $filter) = @_; if($filter->[1] eq '<=') { return( $hash{$filter->[0]} <= $filter->[2] ); } elsif($filter->[1] eq 'eq') { return( $hash{$filter->[0]} eq $filter->[2] ); } else { die "unsupported comparator: $filter->[$index]->[1]"; } } if( match($hash, $filters->[0]) and match($hash, $filters->[1]) and match($hash, $filters->[2]) ) { print OUTFILE $line, "\n"; }

If I was going to use eval, I would definitely check for errors after each eval. I suspect if you did so, you would find that the eval in your second if gives a compile error - but it's only a guess. If you did add error handling to your implementation with eval, I would find it even harder to read than what you have currently, which is already too hard for my taste, but that's just me. In any case, if you want to know why your program isn't doing what you expect, a very good place to start is error handling and reporting. To this end, you might find strict and warnings helpful.


Comment on Re: Problem when comparing strings as opposed to numerical values
Select or Download Code
Re^2: Problem when comparing strings as opposed to numerical values
by dkhalfe (Acolyte) on Jul 19, 2012 at 21:03 UTC

    adding in the single quotes seemed to have fixed the problem. I appreciate the advice and am going to try and implement the 'sub match' code you have provided. Thank you!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-08-28 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (258 votes), past polls