develooper Front page | perl.beginners | Postings from March 2002

Re: A Switch Case for multi-state returns.

Thread Previous | Thread Next
From:
Jonathan E. Paton
Date:
March 31, 2002 19:07
Subject:
Re: A Switch Case for multi-state returns.
Message ID:
20020401030719.21237.qmail@web14608.mail.yahoo.com
> >    If ( $data{$statefield} ) {
> >    # OR If ( $data{uc($rs->Fields('state')->value)} ) {
> >       # Process data
> >    }

Arg!  Don't do that nesting of the if!, use:

if (condition1) {
    #action 1
}
elsif (condition2) {
    #action 2
}
else {
    #default
}

>     SWITCH: {
> 
>          /MA/ && do {
> 			#do MA specific stuff
> 			.....
> 			last SWITCH;
>          } # end do MA stuff
> 
>          /CI/ && do {
> 			#do MA specific stuff
> 			......
> 			last SWITCH;
>          } # end do MA stuff
> 
> 		.....
> 
>          die "Mr Wizard the stateField $statefield unguarded";
> 
>     } # end of switch
> 
> } else {
>          # not in our state List
> }

There is only certain people that appeals to, not including myself.  Those who like it should
notice Damian Conway uses it in "Object Orientated Perl"...

> this way as the @state list grows or shrinks the one simply
> adds in one more switch statement....

What if there is HUNDREDS of items, then it'd be really slow.  For a better approach using hashes
then see what I did in:

http://groups.yahoo.com/group/perl-beginner/message/9583

Remembering you can build your hash like:

my %hash = (
    state1 => sub {
        # Do something
    },

    state2 => sub {
        # Do something
    }
);

Jonathan Paton

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About