If the rest of your intervals are steps of 5 then a little math manipulation does most of the work.

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
Then you only need if statements to catch those below a minimum or greater than the highest key.

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

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.