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

Why won't this code return when the scalars don't match? It always says it's equal no matter what I substitute for $aa.

use strict;
my $a = "A";
my $aa = uc("b");
print "$a\n";
print "$aa\n";
if ($a = $aa)
{
print "it's equal";
} else {
print "its' not equal";
}

Replies are listed 'Best First'.
Re: matching strings
by Happy-the-monk (Canon) on Jun 16, 2004 at 15:35 UTC

    Why won't this code return when the scalars don't match?

    Because you used a "=" instead of an "eq":
    You tested wether you could set   $a   to the value of   $aa   instead of making a comparison.
    See   perldoc perlintro   and   perldoc perlop  for more.

    Cheers, Sören

Re: matching strings
by borisz (Canon) on Jun 16, 2004 at 15:36 UTC
    You assign $aa to $a inside the if so they are equal. To compare them use  $a eq $aa for strings or $a == $aa for numbers.
    Boris
Re: matching strings
by Old_Gray_Bear (Bishop) on Jun 16, 2004 at 15:35 UTC
    Try
    if ($a cmp $aa) ....
    = -- assignment
    == - numeric comparison
    cmp string comparison

    ----
    I Go Back to Sleep, Now.

    OGB

      That's not what cmp is for. It's the string equvilent of <=> and is meant for use in sorting operations. In this case, it won't work at all as shown, because it returns 0 when the two strings are equal (unless( $a cmp $aa) would work, but there is a better way).

      What the OP really wants is eq.

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.