First Perl Program

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

First Perl Program

Post by Muskelmann098 on Sat May 23, 2009 7:20 am
([msg=24222]see First Perl Program[/msg])

Hello folks!

I have very recently taken up programming, and was told that I should learn Perl first. SO I download the Perl Interpreter and start working on a textfile while reading some of the tutorials on the side. After about 3 hours of doing this, I come up with the following code:

Code: Select all
#!D:\Perl\bin\perl        #This is because I use Windows.

use warnings;

BEGIN {
   
    open STDERR, ">>", "error.txt";

}

$x = 1 * 81;
$x /= 9;

print "Welcome to the Math Challenge!\nType your name to continue.\n\n";

$name0 = <>;

print "\nWhat is 81/9?\n\n";

$name1 = <>;

if ($name1 == $x) {
   print "That is correct, the answer is $x\n\n\n";
}

else {
   print "That is wrong, the answer is $x\n";
   print "You FAIL!";
}

if ($x < 0) {
   print "x can not be zero\n";
}

if ($name1 == $x) {
   print "What is 2*2?\n\n";
}

$y = 2 * 2;
$name2 = <>;

if ($name2 == $y) {
   print "That is correct, the answer is $y\n\n\n";
}

else {
   print "That is wrong, the answer is $y\n";
   print "You FAIL!";
}

if ($name2 == $y) {
   print "What is 25*3?\n\n";
}

$z = 25 * 3;
$name3 = <>;

if ($name3 == $z) {
   print "That is correct, the answer is $z\n\n\n";
}

else {
   print "That is wrong, the answer is $z\n";
   print "You FAIL!";
}

if ($name3 == $z) {
   print "What is 35*2?\n\n";
}

$xx = 35 * 2;
$name4 = <>;

if ($name4 == $xx) {
   print "That is correct, the answer is $xx\n\n\n";
}

else {
   print "That is wrong, the answer is $xx\n";
   print "You FAIL!";
}

if ($name4 == $xx) {
   print "Congratulations, $name0!! You Win! \nPress Enter to close window.";
}

$placeholder = <>;


As you see it's very long, and probably not very good, however, it works!! Open in Perl and see for yourself. Anyway, I'm posting it here because I want to improve and learn form experienced programmers. I saw a post about bumming down the lines in the code, so I figured someone might want to try it with this one. I know there are probably many strings and modules that makes the whole thing shorter, but this is my first attempt.

Also, please refrain from stuff like "OMG!! THAT F*ckin SUCKS!!" It's my first day of programming... be patient!
Muskelmann098
Experienced User
Experienced User
 
Posts: 78
Joined: Mon Feb 02, 2009 9:39 am
Blog: View Blog (0)


Re: First Perl Program

Post by BhaaL on Sat May 23, 2009 9:53 am
([msg=24231]see Re: First Perl Program[/msg])

You're overcomplicating stuff as it gets, try to see how things are connected, and where they are used.

Instead of
Code: Select all
$x = 1 * 81;
$x /= 9;

if ($name1 == $x) {
   print "That is correct, the answer is $x\n\n\n";
}
else {
   print "That is wrong, the answer is $x\n";
   print "You FAIL!";
}

You can simply write:
Code: Select all
$x = 81 / 9;
if ($name1 == $x) ...


This check is pretty useless:
Code: Select all
if ($x < 0) {
   print "x can not be zero\n";
}

You yourself set the value for x, and it is never modified, so it should never change; especially not in that place. I think you left that in by mistake, right?

And you might want to put all your success things into the same branch of the first if:
Code: Select all
$input = <>;
if ($input == (81/9)) {
  print "Correct.\nWhat is 2*2?\n";
  $input = <>;
  if ($input == (2*2)) {
    print "Correct.";
   //continue from here, i think you know how.
  }
  else {
    print "Sorry, you failed.\n";
  }
}
else {
print "Sorry, you failed.\n";
}

Note two things:
- $x, $y etc are left out, and put right into the if-Condition. Brackets added for readibility.
- $input is used and re-used, instead of declaring multiple variables.

From the current state of your program, I'd suggest you read further on control structures (if, while, for and the likes), plus move on to subs/functions. After that, you might want to look into arrays and maps.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: First Perl Program

Post by Muskelmann098 on Sat May 23, 2009 12:01 pm
([msg=24241]see Re: First Perl Program[/msg])

Thanks for your help, BhaaL, makes it a lot simpler.

The reason for all the variables is that I started writing this while reading about them, the rest is just added on as I went through the tutorial, trying to include most possible examples.
This check is pretty useless:
Code: Select all
Select all
    if ($x < 0) {
       print "x can not be zero\n";
    }



You yourself set the value for x, and it is never modified, so it should never change; especially not in that place. I think you left that in by mistake, right?

Yes, more or less by mistake, as I said, I was trying to include some examples to see how they worked.

Anyway, thanks for your help. I appreciate it and will surly continue reading on this stuff. I'm a practical mind though, so just reading won't make it stick.
Muskelmann098
Experienced User
Experienced User
 
Posts: 78
Joined: Mon Feb 02, 2009 9:39 am
Blog: View Blog (0)


Re: First Perl Program

Post by BhaaL on Sun May 24, 2009 5:29 am
([msg=24274]see Re: First Perl Program[/msg])

Learning by doing is also a good way to learn of your mistakes ;)
You can't learn of mistakes you never made.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests