It looks to me like the code all does the same thing: creating a frame with a different color and a different balloon message, on some conditions.

I can see basically two options for refactoring:

  1. Put the common code in a sub, and call the sub for every condition
  2. Create a loop with the data that differs between loops, as a function of the loop variable.
I'll produce some code showing both options.

The first option: create a sub and call it.

my $attach_frame = sub { my($color, $label) = @_; my $frame = $tab->Label(-bg => $color, -relief => 'sunken', width +=> 15); $balloon->attach($frame,-balloonmsg => $label); } if ($score eq "NoHit") { $attach_frame->('black', 'No Hits'); } else if($score == 0) { $attach_frame->('red', 'E = 0'); } else if($score < 1e-99) { #eh... nothing? } else if($score < 1e-90) { $attach_frame->('orange', '"1e-99 < E < 1e-90"'); } else if($score < 1e-80) { $attach_frame->('gold', '1e-90 < E < 1e-80'); } else if($score < 1e-70) { $attach_frame->('yellow', '1e-80 < E < 1e-70'); } else if($score < 1e-60) { $attach_frame->('chartreuse', '1e-70 < E < 1e-60'); } else if($score < 1e-50) { $attach_frame->('green', '1e-60 < E < 1e-50'); } else if($score < 1e-40) { $attach_frame->('turquoise', '1e-50 < E < 1e-40'); } else if($score < 1e-30) { $attach_frame->('blue', '1e-40 < E < 1e-30'); } else if($score < 1e-20) { $attach_frame->('pink', '1e-30 < E < 1e-20'); } else if($score < 1e-10) { $attach_frame->('purple', '1e-20 < E < 1e-10'); } else if($score < 1) { $attach_frame->('grey', '1e-10 < E < 1'); }

The second option: create a test loop.

foreach( [black => "NoHit", 'No Hits'], [red => [0]], [orange => ['1e-99', '1e-90']], [gold => ['1e-90', '1e-80']], [yellow => ['1e-80', '1e-70']], [chartreuse => ['1e-70', '1e-60']], [green => ['1e-60', '1e-50']], [turquoise => ['1e-50', '1e-40']], [blue => ['1e-40', '1e-30']], [pink => ['1e-30', '1e-20']], [purple => ['1e-20', '1e-10']], [grey => ['1e-10', '1']]) { my($color, $range, $label) = @$_; my $match; unless(ref $range) { if($match = $score eq $range) { defined $label or $label = $range; } } elsif(@$range == 1) { if($match = $score == $range->[0]) { defined $label or $label = "E = $range->[0]"; } } else { if($match = $score >= $range->[0] && $score < $range->[1]) { defined $label or $label = "$range->[0] < E < $range->[1]" +; } } if($match) { $frame = $tab->Label(-bg => $color, -relief => 'sunken', width + => 15); $balloon->attach($frame,-balloonmsg => $label); last; } }

In reply to Re: doing the same thing but with less code by bart
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.