If the rest of your intervals are steps of 5 then a little math manipulation does most of the work.
Then you only need if statements to catch those below a minimum or greater than the highest key.my %num = (15=>1, 20=>2, 25=>3, 30=>4); print $_, ' maps to ', $num{ ( 1 + int( ($_ - 1) / 5) ) * 5}, $/ for 1 +0 .. 31 10 maps to Use of uninitialized value in print 11 maps to 1 12 maps to 1 13 maps to 1 14 maps to 1 15 maps to 1 16 maps to 2 17 maps to 2 18 maps to 2 19 maps to 2 20 maps to 2 21 maps to 3 22 maps to 3 23 maps to 3 24 maps to 3 25 maps to 3 26 maps to 4 27 maps to 4 28 maps to 4 29 maps to 4 30 maps to 4 31 maps to Use of uninitialized value in print
Update The the light of the additional info you provided above, you could use this (if the size of the hash is fairly small).
for my $num (10,15,16,22,27,30,31) { print $num, $num{ ( grep{ $_ >= $num } sort{ $a <=> $b } keys %num )[0]}, $/; }
Again, you would need a conditional to cover those larger than the highest.
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
In reply to Re: List of Numbers
by BrowserUk
in thread List of Numbers
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |