in reply to Replace $a with $b if $a ne $b

why not:

$a = $b if($a ne $b);

You should not be using $a and $b however, as they are "special" variables used by sort


They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^2: Replace $a with $b if $a ne $b
by ikegami (Patriarch) on May 06, 2005 at 18:27 UTC
    You should not be using $a and $b however, as they are "special" variables used by sort

    Doesn't matter. sort localizes $a and $b.

    $a = 1; print((sort { $b cmp $a } qw( a b c z y x )), $/); print($a, $/); # Prints "1"
      Does too matter. Try this code on perl 5.8.x.
      my $a = 1; print((sort { $b cmp $a } qw( a b c z y x )), $/); print($a, $/); # Prints "1"
Re^2: Replace $a with $b if $a ne $b
by dfgk (Initiate) on Feb 06, 2013 at 10:19 UTC

    $a=$b if ($b ne $a);

    how can i do this another way, like a:

    +=,*=,.=,||=,..., etc

    ps: because my variable $a is very long, for example:

    $a{aaaaaaa1}{aaaaaaa2}{aaaaaaa3}=$b if ($b ne $a{aaaaaaa1}{aaaaaaa2}{aaaaaaa3});

    Thanks beforehand!

      Just do:

      $a = $b;

      The if clause is fairly redundant (unless you're concerned about one of the variables being a blessed object which overloads stringification).

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name