in reply to variable definitions

It's basically because i always seem to get $Updated == "No" in the following
$Updated = "No"; foreach $Line (@Lines) { if ( $WeWantToMakeAChange = "true") { &MakeTheChange; $Updated = "Yes"; } } if ($Updated == "No") { &NoChangesToAnyLinesWereMadeSoNowDoThis; }

&NoChangesToAnyLinesWereMadeSoNowDoThis always seems to run, even though changes are made...

Replies are listed 'Best First'.
Re2: variable definitions
by dragonchild (Archbishop) on Aug 15, 2003 at 13:37 UTC
    Try reading up on basic Perl operators. == is for numbers. eq is for strings. You're comparing strings, so use eq. Both use the same assignment operator, which is =.

    (Confusing explanation coming...) The technical reason why your if-statement keeps running is that strings, when treated as numbers, are equal to 0. Both "Yes" and "No" are numerically 0, and thus numerically equal.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Re: variable definitions
by stajich (Chaplain) on Aug 15, 2003 at 13:40 UTC
    You should probably use strict first off - would catch one of the errors in your code.

    First - you're using the wrong equality tester - if you want to test if a variable is equal to the string "true" use the eq operator.

    This follow line has to be changed for 2 reasons - you are actually doing an assignment - the single equals = means $WeWantToMakeAChange gets the value of 'true'. Second you should use eq to evalue if a string is equal.

    if( $WeWantToMakeAChange eq "true" ) { }
    But why not use 1 for true and 0 for false instead of these strings?
      Damn, fatigue takes its toll again. Thanks folks up and running again 9when im supposed to..) cheers ant