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:
Help!

 



chitira6
Deleted

Sep 3, 2000, 10:57 AM

Post #1 of 5 (592 views)
Help! Can't Post

Hi!,

When I run the below program and I enter
"PERL" the 1st message gets displayed instead of 2nd message.


Thanks in advance


# asklang.pl
#
# This script ask a user a question and gets the response
#
#

print "What language are you using?";

$lan = <STDIN>;

chomp($lan);

$lan = "\U $lan";

print "after uppercase:$lan\n";

if ($lan ne "PERL")
{
print "$lan also a good language, all the best\n";

}
else
{
print "Congratulations, you chose perl\n";
}




BigRich
Novice

Sep 3, 2000, 12:03 AM

Post #2 of 5 (592 views)
Re: Help! [In reply to] Can't Post

When you uppercased $lan, you added a space before it, so it became " PERL" not "PERL".

An easier method would be to do a case insensitive comparison.

print "What language are you using?";

chomp($lan = <STDIN> );

($lan =~ /^Perl$/i) ? print "Congratulations, you chose Perl\n" : print "$lan is also a good language, all the best\n";


Kanji
User / Moderator

Sep 3, 2000, 12:06 AM

Post #3 of 5 (592 views)
Re: Help! [In reply to] Can't Post

You put a space in between \U and $lan, so "PERL" became " PERL". Remove the space.

Perhaps $lan = uc($lan); would be more intuitive?


Kanji
User / Moderator

Sep 3, 2000, 12:13 AM

Post #4 of 5 (592 views)
Re: Help! [In reply to] Can't Post

No need to have print twice in that condition or use a regexp.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

chomp($lan = <STDIN> );
print uc($lan) eq "PERL"
? "Congratulations [..]"
: "$lan is also [..]";</pre><HR></BLOCKQUOTE>


BigRich
Novice

Sep 3, 2000, 1:49 AM

Post #5 of 5 (592 views)
Re: Help! [In reply to] Can't Post

No "need" to uppercase either.

$Perl_motto = "There's more than one way to do it";

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


print "What language are you using?";
chomp($_=<STDIN> );
print /^Perl$/i ? "Congratulations, you chose Perl\n" : "$_ is also a good language, all the best\n";</pre><HR></BLOCKQUOTE>

Not quite as readable to the beginners who may be visiting the beginners forum, but it works.

[This message has been edited by BigRich (edited 09-03-2000).]

 
 


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

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