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

I'm finding it strange encountering this problem. Basically, I'm constructing two variables - $string1 and $string2. When I eval a $query (($string2 eq 'GREAT') && ($minimum eq '78%')) using $string1, it works fine. But when I run the same eval using $string2, it doesn't work. $string1 is a hardcoded string and $string2 is obtained by splitting a longer string. I trimmed the input and then put it in $string2 but still it doesn't seem to work. When I do the following:
if($string1 eq $string2) { print "SAME"; }
It doesn't print out anything. But when i print both the strings on the terminal, they look exactly the same so I suspect something else is going wrong. Can someone kindly help me out please?

Replies are listed 'Best First'.
Re: Its not working.. String works but not split
by Corion (Patriarch) on Mar 08, 2008 at 22:50 UTC

    When Perl says that two things are not equal, Perl is right.

    My guess is that at least one of the two strings has whitespace in front of it or at its end. You should print both out, for example with delimiters at their front and end:

    print "String1: >$string1<\n"; print "String2: >$string2<\n";

    You posted some code in your question, but it would be easier to pinpoint your exact error if you had reduced your code to a small, yet self-contained example that still reproduces the behaviour you observe. I find that in most cases, by reducing my code I already find the error myself. My personal guess is that you left a trailing newline on the string that you read from a file.

Re: Its not working.. String works but not split
by johngg (Canon) on Mar 08, 2008 at 22:57 UTC
    Well, if nothing is printed out it is unlikely that they are the same, notwithstanding appearances. Perhaps there are non-printing characters in one of the strings. You could examine each string character by character, doing it something like this, where to demonstrate I put a NUL in the middle of the string.

    $ perl -le ' > $str = qq{ab\0cd}; > print $str; > print ord for split m{}, $str;' abcd 97 98 0 99 100 $

    Note that the NUL doesn't show up when the string is printed but can be seen when each character's ordinal value is printed.

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Its not working.. String works but not split
by ikegami (Patriarch) on Mar 08, 2008 at 23:34 UTC
    The "PV" lines outputted by the following code will show what Perl sees more clearly.
    use Devel::Peek; Dump($string1); Dump($string2);
Re: Its not working.. String works but not split
by igelkott (Priest) on Mar 08, 2008 at 23:18 UTC
    Might not be trimming $string2 properly, eg, spaces, \r or other characters which are hard to see.

    Some things to try:

    • If you had trimmed with chop, try switching to chomp.
    • Use $string =~ s/^\s*|\s*$//g to remove whitespace before and after (preserves blanks within $string2)
    • If you can limit what should appear in $string2, filter it with something like
      $string2 =~ y/[a-zA-z0-9%,.\$]//cd (adjust as needed)
Re: Its not working.. String works but not split
by Khen1950fx (Canon) on Mar 09, 2008 at 05:48 UTC
    I tried String::Compare.

    #!/usr/bin/perl use strict; use warnings; use String::Compare; my $str1 = "string1 eq 79%"; my $str2 = "string2 eq 79%"; my $points12 = compare($str1, $str2); if($points12 == 1) { print 'They are the same', "\n"; } else { print 'They are not the same', "\n"; }
    updated: The results of compare, if $str1 and $str2 are exactly alike, should return 1; however, it returns less than 1, which simply measures "how much alike they are". And since I wanted to know "if they were exactly alike", I updated the code accordingly. Thanks for noticing that, ikegami