Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I need an quick solution for a little problem I have. I have a code that compare two values written as:

SIEBDB = DISK:\db2_backup\ SIEBDB = USEREXIT

Now when I compare two hash values as

$preprod->{$siebdb} = "DISK:\db2_backup\"; $prod->{$siebdb} = "USEREXIT";

Now when I compare the two values in a hash array,

if($preprod->{$siebdb} != $prod->{$siebdb}){ print "NOT EQUAL" } else{ print "EQUAL"; }

Now when ever I compare those two values I always get them as "EQUAL" instead of "NOT EQUA".

How do I compare the values of string with \ or :

any help pleaassseee !!!

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: pattern match showes true
by jZed (Prior) on Jul 30, 2007 at 20:09 UTC
    "!=" is the for numbers, "ne" is for strings.
Re: pattern match showes true
by ikegami (Patriarch) on Jul 30, 2007 at 20:09 UTC

    != performs a numerical comparison. Since both DISK:\db2_backup\ and USEREXIT evaluate to zero in numerical context, they are numerically equal.

    You want ne which performs a lexical comparison.

    Ref.: perlop

      Hi Thank you for your reply. I tried the 'ne' option but it still doesnt recognize it. I still have atru when I compare those specific values.
        $siebdb = 'somekey'; $preprod->{$siebdb} = "DISK:\\db2_backup\\"; $prod->{$siebdb} = "USEREXIT"; if($preprod->{$siebdb} ne $prod->{$siebdb}) { print "NOT EQUAL"; } else { print "EQUAL"; }

        outputs

        NOT EQUAL

        You should be getting the same.

Re: pattern match showes true
by ysth (Canon) on Jul 30, 2007 at 21:02 UTC
    As others said, != is a numeric comparison; it will compare the numeric value of the left and right operands. Since your strings don't have digits at the beginning, their numeric value will be 0. Taking the numeric value of strings such as yours will trigger a warning if you have warnings enabled (which you should!) to alert you to a possible error on your part.

    ne is the equivalent operator for string comparisons. Similarly, the numeric operators ==, <, >, <=, >=, and <=> have string equivalents eq, lt, gt, le, ge, and cmp.

    Your other problem is using a \ in a double-quoted string. It has a special meaning as an escape character there. Use \\ to get a literal single backslash.

      hi, I am still not able to match the values. I have a problem comparing values like
      $preprod->{$siebdb} = $k; #$k is /siebprod/db13/PRODDB/log2/ $prod->{$siebdb} = $j; # $j is /preprod/db12/PREPRODDB/log2/
      any idea how I should compare these?
        Run the following, and tell us what the output is:

        $preprod->{$siebdb} = $k; print "[$k]\n"; $prod->{$siebdb} = $j; print "[$j]\n"; if ( $preprod->{$siebdb} eq $prod->{$siebdb} ) { print "SAME\n" } else { print "DIFFERENT\n" }

        Do what FunkyMonk said. Also, when you show us what you are doing, you're being just a little bit too terse; seeing larger parts of your actual code might help us help you better (not too large - remove as much as you can while still leaving enough to create the problem).

        I can think of three possible problems you might be having, other than simple syntax misuse: one of your strings might have a newline at the end, making them not exactly equal; $prod and $preprod could be referring to different hashes than you expect; $siebdb could be something different than you expect either when storing the values in the hash or when doing the comparison.

        We'd really have to see more of your code to tell what's up.