On Wed, 5 Dec 2001, Daniel Falkenberg <[EMAIL PROTECTED]> wrote,

> Hey all,
> 
> Please don't stree to much over this one but, is there an easier way of
> writing this?
> 
> while(1){
>   if ($status == 1) {
>     #Do this sub...
>     sub();
>   elsif ($status == 1) {
>     #Do this other sub...
>     sub2();
>   } else {
>     #Continue while loop...
>   }
> }
> 
> Can I make it smaller.  I want to know this to improve my Perl skills.

The basic is having the subs in array, arrange the $status value in which
they occure as their index in the array.

my @subs = (\&sub1, \&sub2, \&sub3);
my $status = get_status_from_somewhere() || 0;
die "bad status: $status\n" unless my $sub = $subs[$status];
$sub->();

There's also alternative to use HASH.  It all depends on your need.


san
-- 
Trabas - http://www.trabas.com



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to