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.
in reply to Re: I need some beginners help in thread I need some beginners help
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>
Re: Re: Re: I need some beginners help
by scain (Curate) on Aug 06, 2001 at 20:36 UTC
|
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] |
|
|
well I changed the chop to chomp and used all caps in randal but
still just says hello randal ??
Betsy
| [reply] |
|
|
| [reply] |
Re: Re: Re: I need some beginners help
by dragonchild (Archbishop) on Aug 06, 2001 at 20:37 UTC
|
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] |
|
|
Well I tried and tried but still isn't working right. I can't
figure it out .
Betsy
| [reply] |
|
|
Take a deep breath, Betsy. Don't despair. We all had problems the first time we tried to run a Perl script.
What you need to do is post the exact code you are currently using, then post exactly what happens on the commandline. No-one here thinks you're dumb or stupid or can't get it. There's probably just one little concept you're confused on and once you see it, the rest will just fall into place.
Did you try running the script I suggested? Maybe that will help.
------ /me wants to be the brightest bulb in the chandelier!
Vote paco for President!
| [reply] |
Re: Re: Re: I need some beginners help
by tachyon (Chancellor) on Aug 06, 2001 at 20:41 UTC
|
$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] |
Re: Re: Re: I need some beginners help
by dga (Hermit) on Aug 06, 2001 at 20:41 UTC
|
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] |
Re: Re: Re: I need some beginners help
by mexnix (Pilgrim) on Aug 06, 2001 at 20:47 UTC
|
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] |
|
|