[ABE.pm] My first en^H^Hclosure doesn't work. :-(

Faber J. Fedor faber at linuxnj.com
Fri Oct 26 13:25:40 PDT 2007


On 26/10/07 15:46 -0400, Walt Mankowski wrote:
> What are you doing wrong?  Well, for starters, whenever you see a long
> string of if/elses like that, you should try to see if you can replace
> it with a loop, or something that's table-driven.  Trying to
> auto-generate the code for the if/elses is just making an already bad
> situation even more complex than it needs to be.

Yes, I know.  

> The original code is also more complex than it needs to be because
> you're checking the lower and upper bound for each range.  Since there
> aren't any holes in the ranges, you only need to check the upper
> range.

Yes, I know. I've already done that as well (see below).

The reason for the *example* is I didn't want to say "I want to
reimplement this if-elsif construct' then show my code that checks
only the upper range and have someone go "where's the elsif?".

> I also don't understand why you want to use a hash when it seems
> simpler to me to use an array.

I like hashes?

> I don't know what an "enclosure" is, but this seems a much simpler
> solution to your problem:

Sorry, "closure" not "enclosure".

>     for my $ar (@costCalcArr) {
>         return $ar->[1] if $capt >= $ar->[0];
>     }

Actually, what I implemented is very close to that:

    foreach my $key (sort { $b <=> $a } keys %costCalcH) {
    return $costCalcH{$key} if $capt >= $key;
    }

the main difference being I don't trust my user to put the values in
proper order.

And the original question remains...

-- 
 
Regards,
 
Faber Fedor
President
Linux New Jersey, Inc.
908-320-0357
800-706-0701

http://www.linuxnj.com




-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



More information about the ABE-pm mailing list