--- Adam Turoff <[EMAIL PROTECTED]> wrote:
> On Tue, May 22, 2001 at 09:37:13AM -0700, Paul wrote:
> > Anybody know if there would likely be any problem with building a
> > "case" statement like the folowing (without installing Switch.pm)?
> >
> >  sub rate ($) {
> >     $_[0] eq 'A' ? .03 :
> >     $_[0] eq 'B' ? .05 :
> >     $_[0] eq 'C' ? .06 :
> >                    .08;   # the default
> >  }
> > Does anyone know of any arbitrary limit on this sort of structure?
> 
> Apparently not:
>   print join("\n", rate('A'), rate('B'), rate('C'), rate('D'),
> undef);
> 
> produces:
>         0.03
>         0.05
>         0.06
>         0.08

Oh, I knew it'd work this deep. I was just thinking ahead for bigger
tables, but this program won't need them. Sorry if I misled you.

> If you were to add more rates, it'll get unmaintainable and confusing
> pretty quickly. 

Absolutely, but this is a known table that can't be changed without
federal agreement, and any change would more likely be simplification
than expansion.

> You're doing simple string comparisons.  Try using
> a hash instead:
> 
>         my %rate = (
>                 A => .03,
>                 B => .05,
>                 C => .06,
>                 default => .08,
>         );
> 
>         my $default_rate = .08;
> 
>         sub rate ($) {
>                 return $rate{$_[0]} || $rate{default};
>         }
> 
> You could also elimiate the function by examining the hash directly
> in your code, depending on how you use it.

Japhy said the same thing. =o)
However, there's a little more code actually in the function, including
other calculated values used in a formula in the assignment, and
retrieval of properties off a deep-bound static object that gets
initialized elsewhere in the program. (Still in beta....) See my
previous response in this thread for a more complete liting of the code
in it's current form.

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply via email to