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.
output: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
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.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
blokhead
In reply to Re: doing the same thing but with less code
by blokhead
in thread doing the same thing but with less code
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |