There must be a more elegant way to do the following:
Any help, comments will be appreciated.# Uses series of if and elsif sub compare { my ($points, $quota) = @_; if ($points > 18000 && $quota < 24) { return 24; } elsif ($points > 16000 && $quota < 23) { return 23; } elsif ($points > 14000 && $quota < 22) { return 22; } elsif ($points > 12000 && $quota < 21) { return 21; } elsif ($points > 10000 && $quota < 20) { return 20; } elsif ($points > 8000 && $quota < 19) { return 19; } elsif ($points > 6000 && $quota < 18) { return 18; } elsif ($points > 4000 && $quota < 17) { #19 return 17; } elsif ($points > 2000 && $quota < 16) { #17 return 16; } return 15; } # attempt to emulate behaviour of compare # not tested sub compare2 { my ($points, $quota) = @_; #my $base_pts = 2000; my $base_quota = 15; while($points > 2000 && $quota > $base_quota) { $points -= 2000; $base_quota += 1; } return $base_quota; }
Thanks!
Update
Thanks once again to all for the solutionis, especially to gsiems who came up with a test on some of the suggested solutions at Re^5: alternatives to if and series of elsif
I expanded the number of solutions using gsiems's test and the results are as follows:
op_cmp2 failed on 16626, 21: returned 21.
davidrw_1 looks ok
davidrw_2 failed on 16626, 21: returned 15.
davidrw_2_5 failed on 16626, 21: returned 22.
ternary_cmp looks ok
Eimi looks ok
kutsu failed on 16626, 21: no value returned.
tlm looks ok
BrowserUK failed on 8256, 22: returned 22.
In reply to alternatives to if and series of elsif by kiat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |