in reply to condensing code

One way:
$test_nums = ($total_nums <= 30) ? 1 : ($total_nums >= 31 and $total_nums <= 60) ? 2 : ($total_nums >= 61 and $total_nums <= 90) ? 3 : ($total_nums >= 91 and $total_nums <= 120) ? 4 : ($total_nums >= 121 and $total_nums <= 150) ? 5 : ($total_nums >= 151 and $total_nums <= 180) ? 6;

Replies are listed 'Best First'.
Re^2: condensing code
by ikegami (Patriarch) on Sep 08, 2006 at 18:15 UTC
    ( Note: I assumed $total_nums contains an integer or that your code was mishandling 30.5 and the like. )

    If you invert the order of the checks, you can simplify to the following.

    $test_nums = $total_nums > 180 ? $test_nums : $total_nums > 150 ? 6 : $total_nums > 120 ? 5 : $total_nums > 90 ? 4 : $total_nums > 60 ? 3 : $total_nums > 30 ? 2 : 1;

    Since the breakpoints form continuous serious of multiples of 30, the above could be simplified further using a loop. Or better yet, using division.

    if ( $total_nums <= 0 ) { $test_nums = 1; } elsif ( $total_nums <= 180 ) { $test_nums = int( ( $total_nums - 1 ) / 30 ) + 1; }

    Update: I got sidetracked while writting my reply, so other people have posted similar solutions already. Please excuse any duplication.

Re^2: condensing code
by Anonymous Monk on Sep 08, 2006 at 17:50 UTC
    How would I write it so that if $total_nums = 1000; I wont have to write all that code and it will loop through all the numbers and be able to give back the number I need?? Thanks for your input.