help using perl

  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

first off this is my very long code..

Code: [ Select ]
print "enter name please: ";
$name = <stdin>;
chomp ($name);

if ($name eq adam) { print "hello" };

until ($name eq adam) { print "enter name please: ";
$name = <stdin>;
chomp ($name) };
if ($name eq adam) { print "welcome " };

print "enter pass please: ";
$pass = <stdin>;
chomp ($pass);

until ($pass eq hiei) { print "enter pass please: ";
$pass = <stdin>;
chomp ($pass) };
if ($pass eq hiei) { print "your clear" };
print "what do you want";
$Q = <stdin>;
chomp ($Q);

if ($Q eq divide) {
print "this will divide any two numbers";

print "put in two numbers";

$divide = <stdin>;
$divide2 = <stdin>;

$divide3 = $divide / $divide2;

print "this is the answer";
print $divide3 };



if ($Q eq "add") { print "this will add any numbers";

$add = <stdin>;
$add2 = <stdin>;

$add3 = $add + $add2;

print $add3; print" is the answer" };



if ($Q eq game) { print "this is a game of chance\n";

print "pick a number: ";

$number1 = <stdin>;
chomp($number1);
$number = int(rand(10)) + 1;
print "you picked $number1\n";

print "the computer picked $number\n";

if($number == $number1)  { print "good job you won\n"; }
if($number != $number1) { print "don't worry this is a hard game\n" } };


if ($Q = game2) { print "this is a word scramble";

print "there will be three words each scrambled.";

print "the first word is aest";

$word = <stdin>;
chomp ($word);

print "the next word is abreb";


$word1 = <stdin>;
chomp ($word1);



print "the final word is aehny";
$word2 = <stdin>;
chomp ($word2);

if ($word eq seat) { print "one right" };
if ($word ne seat) { print "one wrong" };
if ($word1 eq bear) { print "one right" };
if ($word1 ne bear) { print "one wrong" };
if ($word2 eq hyena) { print " one right" };
if ($word2 ne hyena) { print "one wrong" } };
  1. print "enter name please: ";
  2. $name = <stdin>;
  3. chomp ($name);
  4. if ($name eq adam) { print "hello" };
  5. until ($name eq adam) { print "enter name please: ";
  6. $name = <stdin>;
  7. chomp ($name) };
  8. if ($name eq adam) { print "welcome " };
  9. print "enter pass please: ";
  10. $pass = <stdin>;
  11. chomp ($pass);
  12. until ($pass eq hiei) { print "enter pass please: ";
  13. $pass = <stdin>;
  14. chomp ($pass) };
  15. if ($pass eq hiei) { print "your clear" };
  16. print "what do you want";
  17. $Q = <stdin>;
  18. chomp ($Q);
  19. if ($Q eq divide) {
  20. print "this will divide any two numbers";
  21. print "put in two numbers";
  22. $divide = <stdin>;
  23. $divide2 = <stdin>;
  24. $divide3 = $divide / $divide2;
  25. print "this is the answer";
  26. print $divide3 };
  27. if ($Q eq "add") { print "this will add any numbers";
  28. $add = <stdin>;
  29. $add2 = <stdin>;
  30. $add3 = $add + $add2;
  31. print $add3; print" is the answer" };
  32. if ($Q eq game) { print "this is a game of chance\n";
  33. print "pick a number: ";
  34. $number1 = <stdin>;
  35. chomp($number1);
  36. $number = int(rand(10)) + 1;
  37. print "you picked $number1\n";
  38. print "the computer picked $number\n";
  39. if($number == $number1)  { print "good job you won\n"; }
  40. if($number != $number1) { print "don't worry this is a hard game\n" } };
  41. if ($Q = game2) { print "this is a word scramble";
  42. print "there will be three words each scrambled.";
  43. print "the first word is aest";
  44. $word = <stdin>;
  45. chomp ($word);
  46. print "the next word is abreb";
  47. $word1 = <stdin>;
  48. chomp ($word1);
  49. print "the final word is aehny";
  50. $word2 = <stdin>;
  51. chomp ($word2);
  52. if ($word eq seat) { print "one right" };
  53. if ($word ne seat) { print "one wrong" };
  54. if ($word1 eq bear) { print "one right" };
  55. if ($word1 ne bear) { print "one wrong" };
  56. if ($word2 eq hyena) { print " one right" };
  57. if ($word2 ne hyena) { print "one wrong" } };


what i want to do is add that
Code: [ Select ]
print "what do you want";
$Q = <stdin>;
chomp ($Q);
  1. print "what do you want";
  2. $Q = <stdin>;
  3. chomp ($Q);
bit every time a instruction is complete. for example after that adding portion is complete i want it to ask what to do next and wait further instructions. any help for this? thanks
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

You mean you want it to wait at the command prompt for your input before proceeding at each STDIN call? It's already doing that, at least it did for me when I ran that code.

I don't really understand what you're trying to do, exactly, but when I type in your name and password, it seems to work, more or less. You need to look at your formatting a bit.. but the script seems to be working... that is, I didn't get any runtime errors.

.c
  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

ok lets take a part of the code
Code: [ Select ]
print "this will divide any two numbers";

print "put in two numbers";

$divide = <stdin>;
$divide2 = <stdin>;

$divide3 = $divide / $divide2;

print "this is the answer";
print $divide3 };
  1. print "this will divide any two numbers";
  2. print "put in two numbers";
  3. $divide = <stdin>;
  4. $divide2 = <stdin>;
  5. $divide3 = $divide / $divide2;
  6. print "this is the answer";
  7. print $divide3 };
what i want to do is after it complets that i want to make it so it asks me what to do like this

Code: [ Select ]
print "this will divide any two numbers";

print "put in two numbers";

$divide = <stdin>;
$divide2 = <stdin>;

$divide3 = $divide / $divide2;

print "this is the answer";
print $divide3 };
print "what do you want";
$Q = <stdin>;
chomp ($Q);
  1. print "this will divide any two numbers";
  2. print "put in two numbers";
  3. $divide = <stdin>;
  4. $divide2 = <stdin>;
  5. $divide3 = $divide / $divide2;
  6. print "this is the answer";
  7. print $divide3 };
  8. print "what do you want";
  9. $Q = <stdin>;
  10. chomp ($Q);

however if i try it messes up. i want to know if their is another way to do it. hope that clears it up
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Ok, with the exception of an extra curly bracket

Code: [ Select ]
print $divide3 };


I don't really see anything wrong, as long as you put in two integers at the prompt. You'd probably get and error, or something unexpected if you put in a string.

You should at "\n" at the end of each print... it will make the output easier to read, by the way.

I'm going home for the night, I'll take another look at this in the morning if you haven't figured it out yet.

.c
  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

i've redone my code so it will ask you for another command when your done with one

Code: [ Select ]
print "enter name please: ";
$name = <stdin>;
chomp ($name);

if ($name eq adam) { print "welcome" };

until ($name eq adam) { print "enter name please: ";
$name = <stdin>;
chomp ($name) };
if ($name eq adam) { print "welcome " };

print "enter pass please: ";
$pass = <stdin>;
chomp ($pass);

until ($pass eq hiei) { print "enter pass please: ";
$pass = <stdin>;
chomp ($pass) };
if ($pass eq hiei) { print "your clear" };

print "what do you want";
$Q = <stdin>;
chomp ($Q);

if ($Q eq divide) {
print "this will divide any two numbers";

print "put in two numbers";

$divide = <stdin>;
$divide2 = <stdin>;

$divide3 = $divide / $divide2;

print "this is the answer";
print $divide3 };

print "what do you want";
$Q = <stdin>;
chomp ($Q);



if ($Q eq "add") { print "this will add any numbers";

$add = <stdin>;
$add2 = <stdin>;

$add3 = $add + $add2;

print $add3; print" is the answer" };

print "what do you want";
$Q = <stdin>;
chomp ($Q);

if ($Q eq game) { print "this is a game of chance\n";

print "pick a number: ";

$number1 = <stdin>;
chomp($number1);
$number = int(rand(10)) + 1;
print "you picked $number1\n";

print "the computer picked $number\n";

if($number == $number1)  { print "good job you won\n"; }
if($number != $number1) { print "don't worry this is a hard game\n" } };

print "what do you want";
$Q = <stdin>;
chomp ($Q);



if ($Q = game2) { print "this is a word scramble";

print "there will be three words each scrambled.";

print "the first word is aest";

$word = <stdin>;
chomp ($word);

print "the next word is abreb";


$word1 = <stdin>;
chomp ($word1);



print "the final word is aehny";
$word2 = <stdin>;
chomp ($word2);

if ($word eq seat) { print "one right" };
ifa ($word ne seat) { print "one wrong" };
if ($word1 eq bear) { print "one right" };
if ($word1 ne bear) { print "one wrong" };
if ($word2 eq hyena) { print " one right" };
if ($word2 ne hyena) { print "one wrong" } };


print "what do you want";
$Q = <stdin>;
chomp ($Q);



if ($Q eq "exit") { exit };
  1. print "enter name please: ";
  2. $name = <stdin>;
  3. chomp ($name);
  4. if ($name eq adam) { print "welcome" };
  5. until ($name eq adam) { print "enter name please: ";
  6. $name = <stdin>;
  7. chomp ($name) };
  8. if ($name eq adam) { print "welcome " };
  9. print "enter pass please: ";
  10. $pass = <stdin>;
  11. chomp ($pass);
  12. until ($pass eq hiei) { print "enter pass please: ";
  13. $pass = <stdin>;
  14. chomp ($pass) };
  15. if ($pass eq hiei) { print "your clear" };
  16. print "what do you want";
  17. $Q = <stdin>;
  18. chomp ($Q);
  19. if ($Q eq divide) {
  20. print "this will divide any two numbers";
  21. print "put in two numbers";
  22. $divide = <stdin>;
  23. $divide2 = <stdin>;
  24. $divide3 = $divide / $divide2;
  25. print "this is the answer";
  26. print $divide3 };
  27. print "what do you want";
  28. $Q = <stdin>;
  29. chomp ($Q);
  30. if ($Q eq "add") { print "this will add any numbers";
  31. $add = <stdin>;
  32. $add2 = <stdin>;
  33. $add3 = $add + $add2;
  34. print $add3; print" is the answer" };
  35. print "what do you want";
  36. $Q = <stdin>;
  37. chomp ($Q);
  38. if ($Q eq game) { print "this is a game of chance\n";
  39. print "pick a number: ";
  40. $number1 = <stdin>;
  41. chomp($number1);
  42. $number = int(rand(10)) + 1;
  43. print "you picked $number1\n";
  44. print "the computer picked $number\n";
  45. if($number == $number1)  { print "good job you won\n"; }
  46. if($number != $number1) { print "don't worry this is a hard game\n" } };
  47. print "what do you want";
  48. $Q = <stdin>;
  49. chomp ($Q);
  50. if ($Q = game2) { print "this is a word scramble";
  51. print "there will be three words each scrambled.";
  52. print "the first word is aest";
  53. $word = <stdin>;
  54. chomp ($word);
  55. print "the next word is abreb";
  56. $word1 = <stdin>;
  57. chomp ($word1);
  58. print "the final word is aehny";
  59. $word2 = <stdin>;
  60. chomp ($word2);
  61. if ($word eq seat) { print "one right" };
  62. ifa ($word ne seat) { print "one wrong" };
  63. if ($word1 eq bear) { print "one right" };
  64. if ($word1 ne bear) { print "one wrong" };
  65. if ($word2 eq hyena) { print " one right" };
  66. if ($word2 ne hyena) { print "one wrong" } };
  67. print "what do you want";
  68. $Q = <stdin>;
  69. chomp ($Q);
  70. if ($Q eq "exit") { exit };


but it asks you what you want to do twice. do you know how to fix it? try running this and maybe you'll understand what i want to do.
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

I just got slammed with work today... all the writer/editors left on various trips (one to Eritrea, one to Bangledesh and another to Ecuador) and dumped and metric ass ton of work on me today... Very annoying... I probably won't have time to look at this today, unfortunatly.

I noticed a little error, by the way:
ifa ($word ne seat) { print "one wrong" };

Probably not what's causing your problem... but still. Also, while Perl is forgiving, it's good practice to always quote your strings and always teminate with ;

And you don't need to follow a } with a ; ever. For example:
Code: [ Select ]

if($word1 ne "bear") { print "one wrong"; }
if($word2 eq "hyena") { print " one right"; }
if($word2 ne "hyena") { print "one wrong" ;}
  1. if($word1 ne "bear") { print "one wrong"; }
  2. if($word2 eq "hyena") { print " one right"; }
  3. if($word2 ne "hyena") { print "one wrong" ;}


I doubt this is causing your issue. I'm guessing it's a logical flow problem... which takes a little more effort than I can put into it right now... If I have time later, I'll revisit this, sorry I can't be more help...

.c
  • Managedlinks
  • Proficient
  • Proficient
  • Managedlinks
  • Posts: 294

Post 3+ Months Ago

Hi I have gone through the code and found a couple of errors and an unclosed brace. it may have caused your problem
As carnix says you need some line feeds to help format it correctly

Code: [ Select ]
print "enter name please: ";
$name = <stdin>;
chomp ($name);

if ($name eq 'adam') { print "welcome" };

until ($name eq 'adam') { print "enter name please: ";
$name = <stdin>;
chomp ($name) };
if ($name eq 'adam') { print "welcome " };

print "enter pass please: ";
$pass = <stdin>;
chomp ($pass);

until ($pass eq 'hiei')
{
    print "enter pass please: ";
    $pass = <stdin>;
    chomp ($pass) };
    if ($pass eq 'hiei') { print "your clear " };


print "what do you want";
$Q = <stdin>;
chomp ($Q);

if ($Q eq 'divide') {
print "this will divide any two numbers";

print "put in two numbers";

$divide = <stdin>;
$divide2 = <stdin>;
 
$divide3 = $divide / $divide2;

print "this is the answer";
print $divide3 };

print "what do you want";
$Q = <stdin>;
chomp ($Q);



if ($Q eq "add")
{
print "this will add any numbers";

$add = <stdin>;
$add2 = <stdin>;

$add3 = $add + $add2;

print $add3; print" is the answer";
}

print "what do you want";
$Q = <stdin>;
chomp ($Q);

if ($Q eq 'game') { print "this is a game of chance\n";

print "pick a number: ";

$number1 = <stdin>;
chomp($number1);
$number = int(rand(10)) + 1;
print "you picked $number1\n";

print "the computer picked $number\n";

if($number == $number1) { print "good job you won\n"; }
if($number != $number1) { print "don't worry this is a hard game\n" } };

print "what do you want";
$Q = <stdin>;
chomp ($Q);



if ($Q == 'game2') { print "this is a word scramble";

print "there will be three words each scrambled.";

print "the first word is aest";

$word = <stdin>;
chomp ($word);

print "the next word is abreb";


$word1 = <stdin>;
chomp ($word1);



print "the final word is aehny";
$word2 = <stdin>;
chomp ($word2);

if ($word eq 'seat') { print "one right" };
if ($word ne 'seat') { print "one wrong" };
if ($word1 eq 'bear') { print "one right" };
if ($word1 ne 'bear') { print "one wrong" };
if ($word2 eq 'hyena') { print " one right" };
if ($word2 ne 'hyena') { print "one wrong" };
}

print "what do you want";
$Q = <stdin>;
chomp ($Q);



if ($Q eq "exit") { exit };
  1. print "enter name please: ";
  2. $name = <stdin>;
  3. chomp ($name);
  4. if ($name eq 'adam') { print "welcome" };
  5. until ($name eq 'adam') { print "enter name please: ";
  6. $name = <stdin>;
  7. chomp ($name) };
  8. if ($name eq 'adam') { print "welcome " };
  9. print "enter pass please: ";
  10. $pass = <stdin>;
  11. chomp ($pass);
  12. until ($pass eq 'hiei')
  13. {
  14.     print "enter pass please: ";
  15.     $pass = <stdin>;
  16.     chomp ($pass) };
  17.     if ($pass eq 'hiei') { print "your clear " };
  18. print "what do you want";
  19. $Q = <stdin>;
  20. chomp ($Q);
  21. if ($Q eq 'divide') {
  22. print "this will divide any two numbers";
  23. print "put in two numbers";
  24. $divide = <stdin>;
  25. $divide2 = <stdin>;
  26.  
  27. $divide3 = $divide / $divide2;
  28. print "this is the answer";
  29. print $divide3 };
  30. print "what do you want";
  31. $Q = <stdin>;
  32. chomp ($Q);
  33. if ($Q eq "add")
  34. {
  35. print "this will add any numbers";
  36. $add = <stdin>;
  37. $add2 = <stdin>;
  38. $add3 = $add + $add2;
  39. print $add3; print" is the answer";
  40. }
  41. print "what do you want";
  42. $Q = <stdin>;
  43. chomp ($Q);
  44. if ($Q eq 'game') { print "this is a game of chance\n";
  45. print "pick a number: ";
  46. $number1 = <stdin>;
  47. chomp($number1);
  48. $number = int(rand(10)) + 1;
  49. print "you picked $number1\n";
  50. print "the computer picked $number\n";
  51. if($number == $number1) { print "good job you won\n"; }
  52. if($number != $number1) { print "don't worry this is a hard game\n" } };
  53. print "what do you want";
  54. $Q = <stdin>;
  55. chomp ($Q);
  56. if ($Q == 'game2') { print "this is a word scramble";
  57. print "there will be three words each scrambled.";
  58. print "the first word is aest";
  59. $word = <stdin>;
  60. chomp ($word);
  61. print "the next word is abreb";
  62. $word1 = <stdin>;
  63. chomp ($word1);
  64. print "the final word is aehny";
  65. $word2 = <stdin>;
  66. chomp ($word2);
  67. if ($word eq 'seat') { print "one right" };
  68. if ($word ne 'seat') { print "one wrong" };
  69. if ($word1 eq 'bear') { print "one right" };
  70. if ($word1 ne 'bear') { print "one wrong" };
  71. if ($word2 eq 'hyena') { print " one right" };
  72. if ($word2 ne 'hyena') { print "one wrong" };
  73. }
  74. print "what do you want";
  75. $Q = <stdin>;
  76. chomp ($Q);
  77. if ($Q eq "exit") { exit };

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 48 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.