in reply to I need some beginners help

betsyt,

Post some code and we'll look at it. Your question doesn't make much sense without code.

Scott

Replies are listed 'Best First'.
Re: Re: I need some beginners help
by betsyt (Initiate) on Aug 07, 2001 at 00:29 UTC
    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>
      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:

      1. If you're typing within the <code> tags, you don't need to use any other HTML tags, specifically <br>.
      2. 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.)
      3. 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!

        Well I tried and tried but still isn't working right. I can't figure it out . Betsy

      $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

      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.

      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

      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

        well I changed the chop to chomp and used all caps in randal but still just says hello randal ?? Betsy