kal has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Testing whether a value is in a range.
by Limbic~Region (Chancellor) on Mar 10, 2003 at 20:41 UTC | |
|
Re: Testing whether a value is in a range.
by Cody Pendant (Prior) on Mar 10, 2003 at 22:09 UTC | |
by adrianh (Chancellor) on Mar 10, 2003 at 22:29 UTC | |
by Cody Pendant (Prior) on Mar 10, 2003 at 23:02 UTC | |
|
Re: Testing whether a value is in a range.
by adrianh (Chancellor) on Mar 10, 2003 at 22:36 UTC | |
|
Re: Testing whether a value is in a range.
by paulbort (Hermit) on Mar 11, 2003 at 18:32 UTC |