in reply to doing the same thing but with less code

The bulk of all this code repetition seems to be concentrated on determining the size of the number on a logarithmic scale... Instead of brute-force checking a bunch of ranges, you can simply calculate the exponent (1e-XX part) by taking its log base ten. (If you're not aware with the log-identity I've used to get Perl's base-e log function to compute log base 10, consult perldoc -f log)

I take the 10's digit of the exponent as the index of the array of colors, and then code the special cases.. I'll leave it to you to incorporate this into your current code.

my @colors = qw/grey purple pink blue turquoise green chartreuse yellow gold orange/; + for my $score (<DATA>) { chomp $score; my ($color, $label); if ( $score > 0 ) { my $power = int( - log( $score ) / log 10 ); my $tens_place = int( $power / 10 ); $color = $colors[ $tens_place ]; $label = sprintf( "1e-%d0 < E < 1e-%d0", $tens_place+1, $tens_place ); } elsif ( $score eq 'NoHit' ) { $color = 'black'; $label = 'No Hits'; } else { $color = 'red'; $label = 'E = 0'; } + print "$score ==> $color ==> $label\n"; } __DATA__ 1e-10 1e-33 1e-93 NoHit 1e-54 1e-3 0 1e-99
output:
1e-10 ==> purple ==> 1e-20 < E < 1e-10 1e-33 ==> blue ==> 1e-40 < E < 1e-30 1e-93 ==> orange ==> 1e-100 < E < 1e-90 NoHit ==> black ==> No Hits 1e-54 ==> green ==> 1e-60 < E < 1e-50 1e-3 ==> grey ==> 1e-10 < E < 1e-00 0 ==> red ==> E = 0 1e-99 ==> orange ==> 1e-100 < E < 1e-90
This seems to work for my very light test cases. I noticed something odd about your existing code: You have a big foreach loop, but never use the value you're looping over ($_). Maybe this is just a copy-n-paste error, but I thought I'd point it out just in case.

blokhead

Replies are listed 'Best First'.
Re: Re: doing the same thing but with less code
by Anonymous Monk on Aug 17, 2003 at 23:22 UTC
    Thanks to everyone for their replies. I shaved the code down considerably. -Mark