you have code at the bottom that tells it to loop back to the beginning if
your response is not equal to a value between 1 and 3. So, it would continue
to do this until you entered a valid value - or am I misunderstanding the
question?
> unless ($response eq '1' || '2' || '3') {
> print "Enter a number: ";
> chomp($response=<STDIN>);
> }
A more robust example could use a switch statement here instead of nested if
statements, I believe. i.e.
SWITCH: {
$response == 1 && do { your statements here };
$response == 2 && do { your other statements here };
}
-----Original Message-----
From: Chris Zubrzycki [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 18, 2001 4:45 PM
To: [EMAIL PROTECTED]
Subject: testing input/loops
Hey everybody. I have a simple problem that has been stumping me. I'm
using Perl 5.6.1 on Mac OS X 10.1.1. This is a sub for opening a file in
my program. this is the sub, and the problem is when i run it, if i do
not enter a number 1-3, it keeps asking me forever to enter a number.
Even if I enter a number 1-3 after the first run, it keeps asking me to
enter a number.
Thanks for all the help you guys have been giving to everyone, I have
learned much.
sub start {
print "What would like to do?";
newline();
print "1. Start a new file";
newline();
print "2. Append to a file";
newline();
print "3. Close a file";
newline();
print "Enter a number: ";
chomp(my $response=<STDIN>);
if ( $response eq '1' ) {
print "Enter the new filename: ";
chomp(my $newfile=<STDIN>);
while (-e $newfile) {
print "I see you already have a file named
$newfile\n";
print "Please enter a different filename: ";
chomp($newfile=<STDIN>);
}
open(MYFILE, ">$newfile") || die "cannot create: $!";
print_header();
}
elsif ( $response eq '2' ) {
print "Enter the file you wish to applend to: ";
chomp(my $newfile=<STDIN>);
unless (-w $newfile) {
print "I see you already have a file named
$newfile\n";
print "The problem is I can not write to it.\n";
print "Please enter a different filename: ";
chomp($newfile=<STDIN>);
}
open(MYFILE, ">>$newfile") || die "cannot append:
$!";
} elsif ( $response eq '3' ) {
print "Enter the file you wish to close: ";
chomp(my $newfile=<STDIN>);
unless (-w $newfile) {
print "I see you already have a file named
$newfile\n";
print "The problem is I can not write to it.\n";
print "Please enter a different filename: ";
chomp($newfile=<STDIN>);
}
open(MYFILE, ">>$newfile") || die "cannot append:
$!";
print_footer();
} else {
unless ($response eq '1' || '2' || '3') {
print "Enter a number: ";
chomp($response=<STDIN>);
}
}
}
==========================================================================
======
Remember: it's a "Microsoft virus", not an "email virus",
a "Microsoft worm", not a "computer worm".
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]