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 Noobish Problem: IF

 



kolyonl
New User

Sep 18, 2012, 12:33 PM

Post #1 of 3 (795 views)
Extremely Noobish Problem: IF Can't Post

Hi everyone.
I've actually started learning perl yesterday, haven't made much progress, but I read more than I write.

However, I have a very simple question.
I've searched google for operators for non-numerical values, such as "Yes".

This is my script:


Code
say "Please enter first number"; 
my $number1 = <STDIN>;
chomp $number1;
say "Please enter second number";
my $number2 = <STDIN>;
chomp $number2;
my $add = $number1 + $number2;
say "Are you sure you want to add these two numbers together?";
my $yes = <STDIN>;
if ($yes == 1) {
print "The answer is $add \n";
}

else {
print "Okay then, I'll keep it to myself.";
}


If I replace

Code
if ($yes == 1)

with

Code
if ($yes == "Yes")


it says the argument isn't numeric in value. If I use only "=" it says it found a conditional that should be "==". Both changes in the script end up displaying the result whatever the answer is.

tl;dr, Instead of 1 and 0, how do I make it more user-friendly per se with Yes and No.

My guess is I need to predefine $yes, but how can I do so when $yes is defined by <STDIN> ?


(This post was edited by kolyonl on Sep 18, 2012, 12:34 PM)


FishMonger
Veteran / Moderator

Sep 18, 2012, 12:52 PM

Post #2 of 3 (792 views)
Re: [kolyonl] Extremely Noobish Problem: IF [In reply to] Can't Post

You're looking for the 'eq' operator.


Code
if ($yes eq 'Yes')



kolyonl
New User

Sep 18, 2012, 12:54 PM

Post #3 of 3 (790 views)
Re: [FishMonger] Extremely Noobish Problem: IF [In reply to] Can't Post

Thank you. Problem solved. Smile On to the next one.

 
 


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

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