This is an archived low-energy page for bots and other anonmyous visitors.
Please sign up if you are a human and want to interact.
betsyt has asked for the wisdom of the Perl Monks concerning the following question:
I know this may seem very simple to most of you , but I just started
learing perl and just stared reading "Learning Perl" by o'reilly
and am only on Chapter1 and am already stuck. I have typed out
this script and there are no errors but its not working how it
should when you print out the name randal it just says Hello Randal
instead of hello, randal! how good of you to be here! can anyone
tell me what I am doing wrong? If some could help me with this
please just e-mail me at betsy295_99@yahoo.com
Thanks
Re: I need some beginners help
by scain (Curate) on Aug 06, 2001 at 20:02 UTC
|
| [reply] |
|
|
Tried but it looks funny but here you go
#!usr/bin/perl<br>
print "What is your name? ";<br>
$name = <STDIN>;<br>
CHOP($NAME);<br>
IF ($NAME EQ "RANDAL") {<br>
PRINT "HELLO, RANDAL! HOW GOOD OF YOU TO BE HERE!\N";<br>
} ELSE {<br>
PRINT "HELLO, $NAME!\N"; #ORDINARY GREETING<br>
}<br>
| [reply] [d/l] |
|
|
Well, I'll assume that you put in the <br>'s
yourself; you don't have to; <code> is like
<pre>.
Anyway, the code itself looks good, although I would suggest
chomp instead of chop, but that is fairly minor. One thing that
is important to realize is that string comparisons are
case dependent, so if you type "randal", it will not be
equal to "RANDAL". I would guess that is the most likely
cause of your problem. If not, come on back an let us know.
Oh yeah, and Welcome!
Scott
| [reply] [d/l] [select] |
|
|
|
|
|
|
I think I see the problem. When you populate $name, the variable name is lowercase. However, when you do anything with it, it's $NAME, or uppercase. Try doing everything in lowercase and see if it works.
In addition, as scain said, make sure you're typing everything in the same case. See the version of the script I put below for a way to make the script case-insensitive.
A few notes:
- If you're typing within the <code> tags, you don't need to use any other HTML tags, specifically <br>.
- chomp is better than chop, for the purposes you're putting it to. (Getting rid of the newline character(s). If you're on a Windows machine, that may be the reason you're having problems. That script was written for a Unix machine, originally.)
- Although Learning Perl doesn't get into this until a little later, you should start getting into the habit of using strict and warnings. There are a number of nodes on PerlMonks that address this. If I was writing your script from scratch, I'd write it as such:
#!/usr/bin/perl -w
print "What is your name? ";
my $name = <STDIN>;
chomp($name);
if (uc($name) eq 'RANDAL') {
print "Hello, Randal! How good of you to be here!\n";
} else {
print "Hello, $name\n";
}
__END__
Update: Added a few comments after reading scain's response. Added uc to the script listing.
------ /me wants to be the brightest bulb in the chandelier!
| [reply] [d/l] |
|
|
|
|
|
|
$name <-> $NAME do you see the difference? Perl does because it is case sensitive thus $name $Name and $NAME are all different.
Almost all Perl functions like chop if else print eq are lower case. Also \n represents a newline not \N. In short switch the CAPS LOCK OFF!
I presume you added all the <br> tags. You don't need to when posting code like this <code>
#!usr/bin/perl
print "What is your name? ";
$name = <STDIN>;
chop($name);
if ($name eq "RANDAL") {
print "HELLO, RANDAL! HOW GOOD OF YOU TO BE HERE!\n";
} else {
print "HELLO, $name!\n"; #ORDINARY GREETING
}
</code>
You do not see the real code tags because the are doing their job - maintaining all the formatting like <PRE> tags. Just type in the opening code tag, paste in your code and then type in the closing code tag. Have fun - it gets easier. Just remember computers are reather pedantic when it comes to what you feed them!
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [d/l] |
|
|
As for the code part in code tags, your linebreaks are preserved so no need for the BR tags. Also perl is case sensitive, which means your functions and control statements should be lower case (including typing them on here) but that may also be why the working version of this program is acting strange.
You will need to type in Randal exactly, including upper and lower case letters, to get the eq to match in it.
so if your match is
if($name eq "Randal")
you must type Randal and not RANDAL or randal to get a match.
Hope this helps and welcome to the monastery.
| [reply] [d/l] |
|
|
My 2 cents: You need to make all your statements lowercase (i.e. CHOP, IF, PRINT, ELSE). You need to know $name is different from $NAME, so the program will not even recognize $NAME. \N needs to be \n. Case is very important in perl! I'm not even sure what \N does. Other than that, you can enable warnings by making the #!/usr/bin/perl line #!/usr/bin/perl -w. That will show you your errors. Someone else suggested using chomp, so i'll put it in there too. Keep hacking away!
#!usr/bin/perl -w
print "What is your name? ";
$name = <STDIN>;
chomp($name);
if ($name eq "Randal") {
print "HELLO, RANDAL! HOW GOOD OF YOU TO BE HERE!\n";
} else {
print "HELLO, $name!\n"; #ORDINARY GREETING
}
____________________________________________
<moviequote name="Hackers">
The Plague: [...]Well let me explain the New World Order. Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! </moviequote>
mexnix.perlmonk.org | [reply] [d/l] |
Re: I need some beginners help
by VSarkiss (Monsignor) on Aug 06, 2001 at 20:02 UTC
|
Beginner questions are welcome here as much as any other kind. But it's impossible to say why your script isn't working without seeing it. Please retype or copy-and-paste it here and someone will surely be able to help.
| [reply] |
Re: I need some beginners help
by dragonchild (Archbishop) on Aug 06, 2001 at 20:03 UTC
|
Since I don't have Learning Perl in front of me, I can't help you with the specifics.
However, I can give you some suggestions regarding asking for help on PerlMonks.
- Type in the code. Put it between <code> tags, to keep its formatting.
- Tell us what's happening. Again, it's helpful to do so within its own set of <code> tags.
- Tell us what version of Perl (most people are either on 5.005 or 5.6.0) and what type of OS (Windows, Unix, Mac, etc). There are some slight differences between the version of Perl and the OS types, which could affect an answer.
Most importantly, welcome aboard! :)
------ /me wants to be the brightest bulb in the chandelier! | [reply] |
|
|
#!usr/bin/perl
print "What is your name? ";
$name = <STDIN>;
CHOP($NAME);
IF ($NAME EQ "RANDAL") {
PRINT "HELLO, RANDAL! HOW GOOD OF YOU TO BE HERE!\N";
} ELSE {
PRINT "HELLO, $NAME!\N"; #ORDINARY GREETING
}
What is hapening is when I go to dos I type in perl hello.pl
and it says what is your name so I put in Randal and it says hello
randal . And thats it Its supose to say Hellok Randal! How good of you to be here
Thanks
betsy | [reply] [d/l] |
|
|
#!/usr/bin/perl
print "What is your name?";
$name = <STDIN>;
chop($name);
if ($name =~ /RANDAL/i) {
print "HELLO, RANDAL! HOW GOOD OF YOU TO BE HERE\n";
} else {
print "Hello, $name!\n";
}
This is the same code, except on line 5, we change the eq to =~ /RANDAL/i. This uses a Regular Expression to match the case. If you want to test your script type in RANDAL (in the same case as in the script, not randal, but RANDAL), and it will work fine for you.
!/usr/bin/perl
print "What is your name? ";
$name = <STDIN>;
CHOP($NAME);
IF ($NAME EQ "RANDAL") {
PRINT "HELLO, RANDAL! HOW GOOD OF YOU TO BE HERE!\n";
} ELSE {
PRINT "HELLO, $NAME!\N"; #ORDINARY GREETING
}
This code works fine, but when you get the prompt "What is your name?" type RANDAL and it will work correctly for you.
HTH
- f o o g o d
--- ruining the bell curve for everyone else --- | [reply] [d/l] [select] |
|
|