Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: elsif statement not being evaluated

by 2teez (Priest)
on Nov 18, 2012 at 06:27 UTC ( #1004379=note: print w/ replies, xml ) Need Help??


in reply to elsif statement not being evaluated

Hi jonagondos,
check this in your code:

if ( $choice = $an2 ) { ... } elsif ( $choice = $an1 ) { ... }
I suppose you want to use
if ( $choice == $an2 ) { ... } elsif ( $choice == $an1 ) { ... }
You are assigning to variable $choice, the value of the variable $ans2 using the assignment operator '=' .
So, whatsoever, value inputted was replaced by the value of variable $ans2.
In which case variable $choice value is ALWAYS evaluate to 2 the pre-defined value of variable $ans2, so your elsif(){...} statement is not being evaluated.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re: elsif statement not being evaluated
Select or Download Code
Re^2: elsif statement not being evaluated
by jonagondos (Novice) on Nov 18, 2012 at 06:29 UTC
    oh gosh i feel stupid... i was using the assignment operator

      Happens to everyone using C or languages inheriting or stealing from C. But you can make the compiler / interpreter complain loudly if you make sure the constant is on the left side of the operator:

      if ($answer=42) { # $answer is now always 42 ... } if (42=$answer) { # "Can't modify constant item in scalar assignment" ... }

      This won't help when you compare two variables, but in your case, the variables $an1 and $an2 aren't variable, but constant. So make them constant, either by using Readonly or by using constant:

      use Readonly; Readonly my $fortytwo => 42; # note: =>, not = ... if ($fortytwo=$answer) { # "Modification of a read-only value attempte +d" ... }
      use constant FORTYTWO => 42; # note: =>, not = ... if (FORTYTWO=$answer) { # "Can't modify constant item in scalar assign +ment" ... }

      Or, in this trivial case, get rid of $an1 and $an2 and use 1 and 2.

      I prefer Readonly over constant, because Readonly just makes the variables readonly, with no surprises, whereas constant (ab)uses the perl optimizer and implements the constants as functions returning a constant value. This has some nasty side effects documented in Readonly.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      jonagondos,

      You're just human and new to Perl!

      And when you're an expert, just remember someone else will be 'new to Perl'!

      You're on your way...Ed

      "Well done is better than well said." - Benjamin Franklin

        ... You're just human and new to Perl! ...

        Assumptions assumptions :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004379]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-09-16 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (46 votes), past polls