At 11:09 AM 08/08/2001 +0100, Barry Carroll wrote:
>Hi All,
>
>I have the subroutine as follows, if I type any number other than 0 thru 7
>it's fine,
>but if i give a string or a char as an input it of course will not function
>correctly.
>
>What can I put in the first if statement to reject characters
Use a regular-expression match to only accept digits as a valid input.
And as far as this code is concerned....:
> if ($menuChoice >= 0 && $menuChoice <= 7)
> {
> SWITCH:
> {
> if ($menuChoice == 0) {readFile() ; last SWITCH; }
> if ($menuChoice == 1) {displayFile() ; last SWITCH;
>}
> if ($menuChoice == 2) {print "Enter Starting Line
>Number: ";
> chomp ($yPos = <STDIN>);
> print "Print to line no:
>";
> $yOff = <STDIN>;
> displayFile($yPos, $yOff)
<snip snip>
> }
> else
> {
> $lastMsg = "That was not a valid choice!";
> callMenu();
> }
>}
You should optimize this code by using a dispatch table, which is basically
a hash which maps keys to references to subroutines:
%menuChoices = ( '0' => \&readFile,
'1' => \&displayFile,
... etc. etc...
);
Then youd just have to do something like this to emulate a "switch" statement:
if ( $menuChoices{$menuchoice} ) {
# dereference the subroutine mapped to $menuchoice
$menuChoices{$menuchoice}->();
}
else {
$lastMsg = "That was not a valid choice!";
callMenu();
}
HTH..Aloha,
mel
--
mel matsuoka Hawaiian Image Productions
Chief Executive Alphageek (vox)1.808.531.5474
[EMAIL PROTECTED] (fax)1.808.526.4040
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]