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


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.