CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Simple Yes or no prompt

 



psynt555
New User

Nov 23, 2012, 8:01 AM

Post #1 of 4 (1796 views)
Simple Yes or no prompt Can't Post

Hi, I am fairly new to perl although i do know bash quite a bit (they are kinda similiar i think).

This is my code:

for ($count = 0; $count < 8; $count++)
{
my @letters = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

print "$letters[int rand @letters]\t";
}
print "\n";
print "Are you happy with these letters?\n";
print "Type 'y' to move on, or 'n' to get different characters.\n";


It prints a random sequence of 8 chars. I want it to (hence the bottom prints) ask the user if they would like to type y for "that is ok" and move on or n to generate another random sequence for them and ask again.

Can anyone help? ive looked at while, until, unless etc and cant get all of them to work.

THanks


Laurent_R
Veteran / Moderator

Nov 23, 2012, 10:47 AM

Post #2 of 4 (1786 views)
Re: [psynt555] Simple Yes or no prompt [In reply to] Can't Post

Hi,

to start with, I would suggest this syntax:


Code
my @letters = 'a' .. 'z';


and I would declare and initialize @letters only once, outside of the for loop (your are bound to get the same array each time anyway, so it is a waste a resources to do it over again.

You could do something like this (untested):


Code
my $answer; 
while ($answer ne 'O' {
my $password = generate_pass();
print "Are you happy with these letters?\n";
print "Type 'y' to move on, or 'n' to get different characters.\n";
$answer = <>;
chomp $answer;
$answer = uc ($answer);
}


Then, put your code to generates the letters in the generate_pass subroutine. You probably need not only to print the letters, but to store them in a string. For example:


Code
sub generate_pass { 
my $pass = "";
my @letters = 'a' .. 'z';
$pass .= $letters[int rand @letters] foreach (0..7);
print $pass, "\n";
return $pass;
}



psynt555
New User

Nov 23, 2012, 11:07 AM

Post #3 of 4 (1784 views)
Re: [Laurent_R] Simple Yes or no prompt [In reply to] Can't Post

changed your code about a bit and got this

<code>

my $option;
while ($option eq "Y") {
my $password = random_letter();
print "are you happy with these letters? y or n\n";
$option = <>;
chomp $option;
$option = uc ($option);
}

sub random_letter {
my $pass = "";
my @letters = 'a' .. 'z';
$pass .= $letters[int rand @letters] foreach (0..7);
print $pass, "\n";
return $pass;
}
</code>



came up with the error

Use of uninitialized value in string eq at ./scrabble2.pl line 8.


Thanks for taking the time to reply.

ANy ideas why?


Laurent_R
Veteran / Moderator

Nov 23, 2012, 2:52 PM

Post #4 of 4 (1771 views)
Re: [psynt555] Simple Yes or no prompt [In reply to] Can't Post

If you change the beginning to:


Code
my $option = ""; 
while ($option eq "Y") {


you should get rid of this warning: $option needs to be not only declared but also initialized to something for the comparison not to issue this warning (I has the same error in the code I suggested, as I said, it was a quick untested answer).

However, if I understand what you want to do, you probably the test to be the opposite:


Code
my $option = ""; 
while ($option ne "Y") {


(If the person says yes, it means that she or he is satisfied with the letters and you do not want to go through the will loop again, but want to exit it.)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives