I have a problem, trying to find out whether or not a certain value is within a range of values. Basically, I'm working with postal/region codes, but for a lot of countries. Some countries have purely numeric postal codes, some have alpha numeric (an example of a UK postcode might be something like EC1N 4RT), and technically I guess some might be all letters.

Now, I'm trying to match postcodes up with Airports, for delivery of parcels. I have a data file, which is essentially a whitespace separated file (it's actually fixed width, but that's irrelevant.... ;), looking something like:

GB SW1 SW24 LHR

So, the first column is the country code (GB == Great Britain, in this instance), the next two columns are the "range", and the last is the Airport (London Heathrow). This is saying that for any postcode in SW1 to SW24, parcels should be sent to Heathrow.

How to do this?! I'm looking for something fairly algorithmically fast, since I'm going to be checking a lot of data. Most countries are fairly sensible and have purely numeric postal codes, so I think I will probably make that a special case (although, really, it's probably the normal case..). But is there any way of testing along the lines of if ('SW1' < $code && $code < 'SW24') then ..? I can think of something like chopping the text part apart from the numeric part, and doing a number of tests, but it seems like there should be an easier way that I just can't see.

Branewave: while I'm typing this, I have thought of another way; making an array with three values, the code to test, the end points of the range, and then calling sort on it and seeing where my code to test ends up (i.e., if it's in the middle of the sort, it is in the range) - I thin k Perl's default sort algo does exactly what I need. Right, I'm off to test ;) But, are there any better ideas? This might not be very fast... Cheers!


In reply to Testing whether a value is in a range. by kal

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.