Query UK Postcode (Town, Region, Country) (2855 Postcodes)

Share your advanced PureBasic knowledge/code with the community.
IndigoFuzz

Query UK Postcode (Town, Region, Country) (2855 Postcodes)

Post by IndigoFuzz »

Hi all,

Thought I might share this piece of code I've written to aid me in data extraction in a professional environment.

The purpose of the code is to be able to determine which Country a UK postcode is listed in (to assist in a marketing campaign).

Find the source here: https://github.com/IndigoFuzz/PureBasic ... stcode.pbi


An example of use:

Code: Select all

IncludeFile "include/postcode.pbi"

; Create List of Test Postcodes
NewList ToCheck.s()
AddElement(ToCheck()) : ToCheck() = "CF5 1AB" 
AddElement(ToCheck()) : ToCheck() = "NP2 1AB"
AddElement(ToCheck()) : ToCheck() = "ST20 1AB"
AddElement(ToCheck()) : ToCheck() = "BT1 1AB"
AddElement(ToCheck()) : ToCheck() = "DT4 1AB"

; Initialise Postcode Query Module
PostCode::Initialise()

; Query Postcodes
ForEach ToCheck()
  Debug "Checking Postcode: " + ToCheck()
  Debug " - Country: " + PostCode::CountryStr(PostCode::GetCountry(ToCheck()))
  Debug " - Region: " + PostCode::GetRegion(ToCheck())
  Debug " - Town: " + PostCode::GetTown(ToCheck())
  Debug ""
Next

; - Uninitialise Postcode Module
PostCode::Release()
Hope someone finds it useful :)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Query UK Postcode (Town, Region, Country) (2855 Postcode

Post by IdeasVacuum »

Neat code. Can I ask why you didn't use a database? There are approx 43,000 ZIP Codes in the USA.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Query UK Postcode (Town, Region, Country) (2855 Postcode

Post by bosker »

Interesting code. Thanks for posting - but I found a bug.
For example, if you are searching for (say) NP22, it will actually match with NP2.
The problem lies in the Get... functions, with this line..

Code: Select all

 If Left(PostCode$, Len(this\pcodes()\prefix)) = this\pcodes()\prefix
This is because the length of the prefix(which may be shorter) is used to match.

I believe it can be fixed by changing the first 3 lines of the Get... functions to..

Code: Select all

    PostCode$ = UCase(StringField(PostCode$, 1, " "))
    ForEach this\pcodes()
      If PostCode$ = this\pcodes()\prefix
User avatar
the.weavster
Addict
Addict
Posts: 1577
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Query UK Postcode (Town, Region, Country) (2855 Postcode

Post by the.weavster »

Where did you get those codes from? :D

The CB4 code covers a large are of Cambridge from Huntingdon Rd to the A14 but your list returns Impington which is a small village outside of Cambridge. In reality Impington is a CB24 code.
For CB25 you're returning Lode which is probably the smallest village within that area.
For CB5 your list returns Stow-cum-Quy but Stow-cum-Quy is really in CB25 with Lode, again CB5 includes a large part of the city of Cambridge from Jesus Green to out beyond the airport.
CB6 is Downham? Downham is a parish not a village, town or city.

These are just the ones I know off the top of my head :shock:

I hope the rest of your list is in better condition than the details you have for the Cambridge area.
IndigoFuzz

Re: Query UK Postcode (Town, Region, Country) (2855 Postcode

Post by IndigoFuzz »

Thanks for the comments folks :)

@bosker - Yes~ I did realise that after putting it in to practice. When I can I'll update the code to introduce the fix :)

@IdeasVacuum - Using a Database is always going to be the better solution, but with this being such a specific application, it was a lot quicker to just scratch it together in a single include file.

@the.weavster - It was a freely gained list of postcodes so I can't vouch for the accuracy to Towns and and Regions sadly :( The application is purely for establishing postcode to country, but as the region and town data was available in the CSV list I acquired, I quickly put in the functionality to retrieve the values.
Post Reply