I can see basically two options for refactoring:
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |