You could bring your limits and calculated rv's into an array of arrays, and then iterate through it to test outcomes. See below:

use strict; use warnings; my @outcome_tests = ( [ 97, 100, '99' ], [ 93, 97, '95' ], [ 85, 93, '90' ], [ 75, 85, '80' ], [ 65, 75, '70' ], [ 55, 65, '60' ], [ 45, 55, '50' ], [ 35, 45, '40' ], [ 25, 35, '30' ], [ 15, 25, '20' ], [ 7, 15, '10' ], [ 3, 7, '05' ], [ 0, 3, '00' ], ); for( 1 .. 20 ) { # Run the test 20 times. my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. my $rv = ''; TEST: { foreach my $test ( @outcome_tests ) { my( $lower, $upper, $category ) = @{ $test }; if( $outcome > $lower and $outcome <= $upper ) { $rv .= $category; last TEST; } } die "ERROR: $outcome is out of bounds.\n"; } print "$outcome => $rv\n"; }

One advantage to this solution is that it turns your limits and categories into data, rather than code. You can add or remove limit pairs without altering the code, and without affecting complexity.


Dave


In reply to Re: Efficiency & Maintenance: if/elsif or Other Dispatch Method by davido
in thread Efficiency & Maintenance: if/elsif or Other Dispatch Method by madbombX

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.