CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Extremely Easy

 



Pro_4
User

Apr 30, 2001, 4:14 PM

Post #1 of 4 (540 views)
Extremely Easy Can't Post

This is really easy but for some reason it just isnt working for me.

How come this if else block isnt working....

if $answer = " I dont know " {
print " That is wrong, BAM, you were launched into the eternal abyss ";
} else {
print " That is correct! You may cross the bridge "
}

Thanks in advance ( sorry i couldnt find a forum for.... the answer is probably so extremely easy i am probably going to beat myself with a baseball bat forum )

:)



People Engrossed by a Reliable Language


wickedxter
User

Apr 30, 2001, 6:56 PM

Post #2 of 4 (534 views)
Re: Extremely Easy [In reply to] Can't Post

try using this..

if($answer eq "I dont know"){
print " That is wrong, BAM, you were launched into the eternal abyss ";
} else {
print " That is correct! You may cross the bridge "
}

dont use the " = " if your going to use an else b/c it wont work, b/c to perl that looks like if $answer (is queal to) " I dont know " ) { print "what ever"; }

and it wont use the else... i hope this helps ya.. it also it wont help ya any by beating your self with a baseball bat forum...

GL



Jasmine
Administrator / Moderator

Apr 30, 2001, 7:25 PM

Post #3 of 4 (533 views)
Re: Extremely Easy [In reply to] Can't Post

The problem is threefold:

1. The lack of parenthesis around the if statement

2. The use of a single = (assignment) instead of == (numeric comparison)

3. Even if you meant ==, you're checking text, not numbers, which means that you should use the asciibetical comparison operator eq.


Code
if ( $text eq "I don't know" ){ 
print "Wrong!";
}
else {
print "Correct!!";
}

You can use either of the comparison operators (== or eq) in an if ... else statement, but be sure to use 2 equals signs, or perl will think that you're assigning $text to be "I don't know" (always true, unless $text is read-only) instead of seeing if $text is equal to "I don't know".

Hope this helps!



Pro_4
User

May 1, 2001, 9:05 AM

Post #4 of 4 (523 views)
Re: Extremely Easy [In reply to] Can't Post

Alright cool, i was having problems with getting the text to work right. It would always say Blah is a bareword and i didnt know what to do.

Thanks

People Engrossed by a Reliable Language

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives