At 02:52 AM 08/08/2001, Mel Matsuoka wrote:
>
>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();
>}

I should have also mentioned that doing it this way eliminates the need to
even have to perform the regular-expression match I mentioned as well
eliminates the need for your first "if" statement (i.e. if ($menuChoice >=
0 && $menuChoice <= 7)). 

Since the only valid values exist as keys in the dispatch hash, anything
else would evaluate as false. This will streamline your code, bigtime :)

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]

Reply via email to