in reply to Re: How to set relational operators to variables to be used by program
in thread How to set relational operators to variables to be used by program

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

This seems to work. Thanks! If any one else has other suggestions please inform!

  • Comment on Re^2: How to set relational operators to variables to be used by program
  • Download Code

Replies are listed 'Best First'.
Re^3: How to set relational operators to variables to be used by program
by jethro (Monsignor) on Jul 19, 2012 at 17:27 UTC

    The usual warning: If you use eval, you have to make sure that either no evildoer can put any code into the file where the opcodes are stored or that you check the values with regexes so that only acceptable ops get through

    And something else: If you use $hash{$filter[0]->[0]} inside the eval string, it is substituted with its value as well. This is fine as long as you want to compare numbers, because (10 < 20) is a valid comparision expression in perl, but if you want to also compare strings, for example "blue" and "green", then the eval will fail because (blue lt green) is not a valid expression, it should be ("blue" lt "green"). Solution: Escape the operands like this: \$hash{\$filter[0]->[0]}

    And your example filter 3 (for column b) is wrong, '=' is assignement, '==' is equality (which would suggest that a regex to check for correct relationships/operators might not be a bad idea anyway)

      How would I go about checking the vlaues with regexes?

        You asked me per /msg, why adding "\" in front of your variables is still not working.

        Well, generally it is. See this short test script:

        #!/usr/bin/perl use strict; use warnings; my $x="a"; my %hash; $hash{"a"}= "ding"; $hash{"b"}= "junk"; $hash{"c"}= "ding"; for my $right ("a","b","c") { if (eval "\$hash{\$x} eq \$hash{\$right}") { print "yes\n"; } else { if ($@ ne "") { print "ERROR: $@\n"; } else { print "no\n";} } }

        try to remove the "\" inside the eval and you will see error messages instead

        If your code is not working, something else must be wrong. Maybe you have a typo or your variables are constructed differently than you think. Check variable values, i.e. print them out, check that they don't have \n at the end... Check if there are errors, and if yes, try to read them carefully

        To your question about checking the values, see BrowserUKs post, his script does check. Simpler methods are for example:

        #regex way if ($op=~m/^(==|eq|<|>)$/) { ... #hash way my %allowedops=( '=='=>1 , 'eq'=>1 , '>'=>1 , '<'=>1 ); if ($allowedops{$op}) { ...
Re^3: How to set relational operators to variables to be used by program
by cheekuperl (Monk) on Jul 19, 2012 at 16:31 UTC
    For that matter, you can also write:
    if (eval "$hash{$filter[0]->[0]} $filter [0]->[1] $filter[0]->[2]") { print OUTFILE $line, "\n"; }
    eliminating the extra for loop.