Best way to put this country list into my program?

Just starting out? Need help? Post your questions and find answers here.
User avatar
the.weavster
Addict
Addict
Posts: 1577
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Best way to put this country list into my program?

Post by the.weavster »

Mijikai wrote:
the.weavster wrote:If you hard code this in what are you going to do when the UK splits up?
Imo this belongs in a SQLite file not embedded in the executable.
Just unload the list into a db.
But its really not that complicated to edit the list, rebuild the data and pack it...
The fact it's not complicated for the developer to edit the list and rebuild is not really the point. What about the users? If the data could change don't hard code it in.
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Best way to put this country list into my program?

Post by Mijikai »

the.weavster wrote:
Mijikai wrote:
the.weavster wrote:If you hard code this in what are you going to do when the UK splits up?
Imo this belongs in a SQLite file not embedded in the executable.
Just unload the list into a db.
But its really not that complicated to edit the list, rebuild the data and pack it...
The fact it's not complicated for the developer to edit the list and rebuild is not really the point. What about the users? If the data could change don't hard code it in.
How about this JSON version? :)
Code:

Code: Select all

;WORLD COUNTRIES (ISO 3166-1 - 2019 / alpha2 / alpha3 / numeric)
;WC - MODULE v.alpha
;Author: Mijikai

DeclareModule WC
  Declare.i Init()
  Declare.i Countries()
  Declare.i SortABC()
  Declare.i SortNumeric()
  Declare.i StartQuery()
  Declare.i NextCountry()
  Declare.s Name(*Country)
  Declare.s Alpha2(*Country)
  Declare.s Alpha3(*Country)
  Declare.i Numeric(*Country)
  Declare.i Flag(*Country)
  Declare.i FindCountry(Name.s)
  Declare.i FindAlpha2(Alpha.s)
  Declare.i FindAlpha3(Alpha.s)
  Declare.i FindNumeric(Value.i)
  Declare.i Release()
EndDeclareModule

Module WC
  
  EnableExplicit
  
  UsePNGImageDecoder()
  
  Structure COUNTRY_STRUCT
    id.w
    name.s{128}
    alpha2.s{2}
    alpha3.s{3}
    numeric.w
    flagsize.w
    Array flag.b(0)
    image.i
  EndStructure
  
  Global NewList country.COUNTRY_STRUCT()
  
  Procedure.i Init()
    Protected handle.i
    handle = CatchJSON(#PB_Any,?wc_start,?wc_end - ?wc_start,#PB_JSON_NoCase )
    If IsJSON(handle)
      ExtractJSONList(JSONValue(handle),country())
      FreeJSON(handle)
      ForEach country()
        country()\image = CatchImage(#PB_Any,country()\flag(),country()\flagsize)
        If Not IsImage(country()\image)
          handle = #Null
          Break
        EndIf 
      Next
      If handle = #Null
        ForEach country()
          If IsImage(country()\image)
            FreeImage(country()\image)
          EndIf
        Next
        ClearList(country())
      EndIf
    EndIf 
    ProcedureReturn ListSize(country())
  EndProcedure
  
  Procedure.i Countries()
    ProcedureReturn ListSize(country())
  EndProcedure
  
  Procedure.i SortABC()
    SortStructuredList(country(),#PB_Sort_Ascending,OffsetOf(COUNTRY_STRUCT\id),#PB_Word)
  EndProcedure
  
  Procedure.i SortNumeric()
    SortStructuredList(country(),#PB_Sort_Ascending,OffsetOf(COUNTRY_STRUCT\numeric),#PB_Word)
  EndProcedure
  
  Procedure.i StartQuery()
    ResetList(country())
  EndProcedure
  
  Procedure.i NextCountry()
    ProcedureReturn NextElement(country())
  EndProcedure
  
  Procedure.s Name(*Country.COUNTRY_STRUCT)
    If *Country
      ProcedureReturn *Country\name
    EndIf 
  EndProcedure
  
  Procedure.s Alpha2(*Country.COUNTRY_STRUCT)
    If *Country
      ProcedureReturn *Country\alpha2
    EndIf 
  EndProcedure
  
  Procedure.s Alpha3(*Country.COUNTRY_STRUCT)
    If *Country
      ProcedureReturn *Country\alpha3
    EndIf 
  EndProcedure
  
  Procedure.i Numeric(*Country.COUNTRY_STRUCT)
    If *Country
      ProcedureReturn *Country\numeric
    EndIf 
  EndProcedure
  
  Procedure.i Flag(*Country.COUNTRY_STRUCT)
    If *Country
      ProcedureReturn *Country\image
    EndIf 
  EndProcedure
  
  Procedure.i FindCountry(Name.s)
    ForEach country()
      If FindString(country()\name,Name)
        ProcedureReturn country()
      EndIf  
    Next
  EndProcedure
  
  Procedure.i FindAlpha2(Alpha.s)
    ForEach country()
      If country()\alpha2 = UCase(Alpha)
        ProcedureReturn country()
      EndIf  
    Next
  EndProcedure
  
  Procedure.i FindAlpha3(Alpha.s)
    ForEach country()
      If country()\alpha3 = UCase(Alpha)
        ProcedureReturn country()
      EndIf  
    Next
  EndProcedure
  
  Procedure.i FindNumeric(Value.i)
    ForEach country()
      If country()\numeric = Value
        ProcedureReturn country()
      EndIf  
    Next
  EndProcedure
  
  Procedure.i Release()
    If ListSize(country())
      ForEach country()
        If IsImage(country()\image)
          FreeImage(country()\image)
        EndIf
      Next
      ClearList(country())
    EndIf
  EndProcedure
    
  DataSection
    wc_start:
    ;download text and save as json from here: https://pastebin.com/SnrBrTPU
    IncludeBinary "country.json"
    wc_end:
  EndDataSection
  
EndModule

EnableExplicit

Global *Country

If WC::Init()
  
  WC::StartQuery()
  ;WC::SortNumeric()
  ;WC::SortABC()
  Repeat
    *Country = WC::NextCountry()
    If *Country
      Debug WC::Name(*Country)
      Debug WC::Numeric(*Country)
    Else
      Break
    EndIf 
  ForEver
  
  Debug "-> FIND DEMO!"
  
  *Country = WC::FindCountry("Germany")
  Debug WC::Name(*Country)
  Debug WC::Alpha2(*Country)
  Debug WC::Alpha3(*Country)
  Debug WC::Numeric(*Country)
  Debug SaveImage(WC::Flag(*Country),"flag.bmp",#PB_ImagePlugin_BMP)
  
  WC::Release()
EndIf

End
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Best way to put this country list into my program?

Post by Tenaja »

the.weavster wrote: The fact it's not complicated for the developer to edit the list and rebuild is not really the point. What about the users? If the data could change don't hard code it in.
I agree with this, if updates might happen, and the program has a long life expectancy. Since the 70s a handful of countries changed names (8 to 10 I think), some going back and forth. On the other hand, if it's only expected to last five years, maybe go with simple.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Best way to put this country list into my program?

Post by TI-994A »

the.weavster wrote:What about the users? If the data could change don't hard code it in.
Keeping program and data separate is part of good coding practices. But, for developers who'd prefer portability without dependencies, it's absolutely fine to integrate resources directly into binaries as a single executable package.

Application data should not fall in the user domain. For safety and stability, any and all updates should be pulled only from the developer.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
the.weavster
Addict
Addict
Posts: 1577
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Best way to put this country list into my program?

Post by the.weavster »

TI-994A wrote:Application data should not fall in the user domain. For safety and stability, any and all updates should be pulled only from the developer.
Would you really call that an "update"? Imo data like this that has the potential to change should be user updatable, there should be input screens with data validation routines etc...
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Best way to put this country list into my program?

Post by TI-994A »

the.weavster wrote:
TI-994A wrote:Application data should not fall in the user domain. For safety and stability, any and all updates should be pulled only from the developer.
Would you really call that an "update"? Imo data like this that has the potential to change should be user updatable, there should be input screens with data validation routines etc...
You're right, and I have no real arguments. It's just that there are as many application models as there are update models. Some are able to minimally patch installations through delta updates, while others might require full re-installs. Granted, external dependencies are ideal for dynamically evolving application data, and are easier to pull and update. But they stifle portability, and could also be prone to misplacement and corruption. We're not talking about configuration or preference files, or even user data for that matter; only application-specific data, which should usually not be tampered or altered by users.

Ultimately, there are no hard rights or wrongs; just developer preferences. IMHO.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Best way to put this country list into my program?

Post by Marc56us »

For this kind of program, and if it is necessary to ensure sustainability, I would tend to mix the two techniques: The original data in the EXE (200 lines of data this is nothing) AND I add a system that tests the possible presence of a data file (text, therefore editable). If the data file exists in the directory, then it is used first.

The search for the "single exe" is no longer necessary since the floppy disks no longer exist and the slightest $2 USB key makes several GB

:wink:
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: Best way to put this country list into my program?

Post by vwidmer »

Thanks everyone again for all the feed back this is what I ended up with and please feel free to correct/comment/enhance/use:

inc.regexpressions.pbi

Code: Select all

Procedure ExtractRegExMatch (text$, Array result$(1), regex$ = "")
  Static iRegEx
  Protected iRetVal = -1 ; regex error
  
  If regex$
    If iRegEx > 0
      FreeRegularExpression(iRegEx)
    EndIf     
    iRegEx = CreateRegularExpression(#PB_Any, regex$)
  EndIf
  
  If iRegEx
    iRetVal = ExtractRegularExpression(iRegEx, text$, result$())   
  EndIf
  
  ProcedureReturn iRetVal
EndProcedure

Procedure.s ExtractRegExString (text$, regex$ = "")
  Dim result$(0)
  ExtractRegExMatch (text$, result$(), regex$)
  ProcedureReturn result$(0)
EndProcedure
inc.isocountry.pbi

Code: Select all

XIncludeFile "inc.regexpressions.pbi"

Global NewList lCountryCodes.s()
AddElement(lCountryCodes()):lCountryCodes() = "AD|Andorra"
AddElement(lCountryCodes()):lCountryCodes() = "AE|United Arab Emirates"
AddElement(lCountryCodes()):lCountryCodes() = "AF|Afghanistan"
AddElement(lCountryCodes()):lCountryCodes() = "AG|Antigua and Barbuda"
AddElement(lCountryCodes()):lCountryCodes() = "AI|Anguilla"
AddElement(lCountryCodes()):lCountryCodes() = "AL|Albania"
AddElement(lCountryCodes()):lCountryCodes() = "AM|Armenia"
AddElement(lCountryCodes()):lCountryCodes() = "AN|Netherlands Antilles"
AddElement(lCountryCodes()):lCountryCodes() = "AO|Angola"
AddElement(lCountryCodes()):lCountryCodes() = "AQ|Antarctica"
AddElement(lCountryCodes()):lCountryCodes() = "AR|Argentina"
AddElement(lCountryCodes()):lCountryCodes() = "AS|American Samoa"
AddElement(lCountryCodes()):lCountryCodes() = "AT|Austria"
AddElement(lCountryCodes()):lCountryCodes() = "AU|Australia"
AddElement(lCountryCodes()):lCountryCodes() = "AW|Aruba"
AddElement(lCountryCodes()):lCountryCodes() = "AZ|Azerbaijan"
AddElement(lCountryCodes()):lCountryCodes() = "BA|Bosnia and Herzegovina"
AddElement(lCountryCodes()):lCountryCodes() = "BB|Barbados"
AddElement(lCountryCodes()):lCountryCodes() = "BD|Bangladesh"
AddElement(lCountryCodes()):lCountryCodes() = "BE|Belgium"
AddElement(lCountryCodes()):lCountryCodes() = "BF|Burkina Faso"
AddElement(lCountryCodes()):lCountryCodes() = "BG|Bulgaria"
AddElement(lCountryCodes()):lCountryCodes() = "BH|Bahrain"
AddElement(lCountryCodes()):lCountryCodes() = "BI|Burundi"
AddElement(lCountryCodes()):lCountryCodes() = "BJ|Benin"
AddElement(lCountryCodes()):lCountryCodes() = "BM|Bermuda"
AddElement(lCountryCodes()):lCountryCodes() = "BN|Brunei Darussalam"
AddElement(lCountryCodes()):lCountryCodes() = "BO|Bolivia"
AddElement(lCountryCodes()):lCountryCodes() = "BR|Brazil"
AddElement(lCountryCodes()):lCountryCodes() = "BS|Bahamas"
AddElement(lCountryCodes()):lCountryCodes() = "BT|Bhutan"
AddElement(lCountryCodes()):lCountryCodes() = "BV|Bouvet Island"
AddElement(lCountryCodes()):lCountryCodes() = "BW|Botswana"
AddElement(lCountryCodes()):lCountryCodes() = "BY|Belarus"
AddElement(lCountryCodes()):lCountryCodes() = "BZ|Belize"
AddElement(lCountryCodes()):lCountryCodes() = "CA|Canada"
AddElement(lCountryCodes()):lCountryCodes() = "CB|Scott Base"
AddElement(lCountryCodes()):lCountryCodes() = "CC|Cocos / Keeling Islands"
AddElement(lCountryCodes()):lCountryCodes() = "CD|Congo, Democratic Repulic of the"
AddElement(lCountryCodes()):lCountryCodes() = "CF|Central African Republic"
AddElement(lCountryCodes()):lCountryCodes() = "CG|Congo, Republic of the"
AddElement(lCountryCodes()):lCountryCodes() = "CH|Switzerland"
AddElement(lCountryCodes()):lCountryCodes() = "CI|Cote dIvoire (Ivory Coast)"
AddElement(lCountryCodes()):lCountryCodes() = "CK|Cook Islands"
AddElement(lCountryCodes()):lCountryCodes() = "CL|Chile"
AddElement(lCountryCodes()):lCountryCodes() = "CM|Cameroon"
AddElement(lCountryCodes()):lCountryCodes() = "CN|China"
AddElement(lCountryCodes()):lCountryCodes() = "CO|Colombia"
AddElement(lCountryCodes()):lCountryCodes() = "CR|Costa Rica"
AddElement(lCountryCodes()):lCountryCodes() = "CU|Cuba"
AddElement(lCountryCodes()):lCountryCodes() = "CV|Cape Verde"
AddElement(lCountryCodes()):lCountryCodes() = "CW|Curacao"
AddElement(lCountryCodes()):lCountryCodes() = "CX|Christmas Island"
AddElement(lCountryCodes()):lCountryCodes() = "CY|Cyprus"
AddElement(lCountryCodes()):lCountryCodes() = "CZ|Czech Republic"
AddElement(lCountryCodes()):lCountryCodes() = "DE|Germany"
AddElement(lCountryCodes()):lCountryCodes() = "DJ|Djibouti"
AddElement(lCountryCodes()):lCountryCodes() = "DK|Denmark"
AddElement(lCountryCodes()):lCountryCodes() = "DM|Dominica"
AddElement(lCountryCodes()):lCountryCodes() = "DO|Dominican Republic"
AddElement(lCountryCodes()):lCountryCodes() = "DZ|Algeria"
AddElement(lCountryCodes()):lCountryCodes() = "EC|Ecuador"
AddElement(lCountryCodes()):lCountryCodes() = "EE|Estonia"
AddElement(lCountryCodes()):lCountryCodes() = "EG|Egypt"
AddElement(lCountryCodes()):lCountryCodes() = "ER|Eritrea"
AddElement(lCountryCodes()):lCountryCodes() = "ES|Spain"
AddElement(lCountryCodes()):lCountryCodes() = "ET|Ethiopia"
AddElement(lCountryCodes()):lCountryCodes() = "FI|Finland"
AddElement(lCountryCodes()):lCountryCodes() = "FJ|Fiji"
AddElement(lCountryCodes()):lCountryCodes() = "FK|Falkland Islands"
AddElement(lCountryCodes()):lCountryCodes() = "FM|Micronesia"
AddElement(lCountryCodes()):lCountryCodes() = "FO|Faroe Islands"
AddElement(lCountryCodes()):lCountryCodes() = "FR|France"
AddElement(lCountryCodes()):lCountryCodes() = "GA|Gabon"
AddElement(lCountryCodes()):lCountryCodes() = "GB|United Kingdom"
AddElement(lCountryCodes()):lCountryCodes() = "GD|Grenada"
AddElement(lCountryCodes()):lCountryCodes() = "GE|Georgia"
AddElement(lCountryCodes()):lCountryCodes() = "GF|French Guiana"
AddElement(lCountryCodes()):lCountryCodes() = "GG|Guernsey"
AddElement(lCountryCodes()):lCountryCodes() = "GH|Ghana"
AddElement(lCountryCodes()):lCountryCodes() = "GI|Gibraltar"
AddElement(lCountryCodes()):lCountryCodes() = "GL|Greenland"
AddElement(lCountryCodes()):lCountryCodes() = "GM|Gambia"
AddElement(lCountryCodes()):lCountryCodes() = "GN|Guinea"
AddElement(lCountryCodes()):lCountryCodes() = "GP|Guadeloupe"
AddElement(lCountryCodes()):lCountryCodes() = "GQ|Equatorial Guinea"
AddElement(lCountryCodes()):lCountryCodes() = "GR|Greece"
AddElement(lCountryCodes()):lCountryCodes() = "GS|South Georgia"
AddElement(lCountryCodes()):lCountryCodes() = "GT|Guatemala"
AddElement(lCountryCodes()):lCountryCodes() = "GU|Guam"
AddElement(lCountryCodes()):lCountryCodes() = "GW|Guinea-Bissau"
AddElement(lCountryCodes()):lCountryCodes() = "GY|Guyana"
AddElement(lCountryCodes()):lCountryCodes() = "HK|Hong Kong, China"
AddElement(lCountryCodes()):lCountryCodes() = "HM|Heard and McDonald Islands"
AddElement(lCountryCodes()):lCountryCodes() = "HN|Honduras"
AddElement(lCountryCodes()):lCountryCodes() = "HR|Croatia"
AddElement(lCountryCodes()):lCountryCodes() = "HT|Haiti"
AddElement(lCountryCodes()):lCountryCodes() = "HU|Hungary"
AddElement(lCountryCodes()):lCountryCodes() = "ID|Indonesia"
AddElement(lCountryCodes()):lCountryCodes() = "IE|Ireland"
AddElement(lCountryCodes()):lCountryCodes() = "IL|Israel"
AddElement(lCountryCodes()):lCountryCodes() = "IM|Isle of Man"
AddElement(lCountryCodes()):lCountryCodes() = "IN|India"
AddElement(lCountryCodes()):lCountryCodes() = "IO|British Indian Ocean Territory"
AddElement(lCountryCodes()):lCountryCodes() = "IQ|Iraq"
AddElement(lCountryCodes()):lCountryCodes() = "IR|Iran"
AddElement(lCountryCodes()):lCountryCodes() = "IS|Iceland"
AddElement(lCountryCodes()):lCountryCodes() = "IT|Italy"
AddElement(lCountryCodes()):lCountryCodes() = "JE|Jersey"
AddElement(lCountryCodes()):lCountryCodes() = "JM|Jamaica"
AddElement(lCountryCodes()):lCountryCodes() = "JO|Jordan"
AddElement(lCountryCodes()):lCountryCodes() = "JP|Japan"
AddElement(lCountryCodes()):lCountryCodes() = "KA|Kerguelen Archipelago"
AddElement(lCountryCodes()):lCountryCodes() = "KE|Kenya"
AddElement(lCountryCodes()):lCountryCodes() = "KG|Kyrgyzstan"
AddElement(lCountryCodes()):lCountryCodes() = "KH|Cambodia"
AddElement(lCountryCodes()):lCountryCodes() = "KI|Kiribati"
AddElement(lCountryCodes()):lCountryCodes() = "KM|Comoros"
AddElement(lCountryCodes()):lCountryCodes() = "KN|St Kitts and Nevis"
AddElement(lCountryCodes()):lCountryCodes() = "KP|Korea, Democratic Peoples Republic of"
AddElement(lCountryCodes()):lCountryCodes() = "KR|Korea, Republic of"
AddElement(lCountryCodes()):lCountryCodes() = "KW|Kuwait"
AddElement(lCountryCodes()):lCountryCodes() = "KY|Cayman Islands"
AddElement(lCountryCodes()):lCountryCodes() = "KZ|Kazakhstan"
AddElement(lCountryCodes()):lCountryCodes() = "LA|Lao Peoples Democratic Republic"
AddElement(lCountryCodes()):lCountryCodes() = "LB|Lebanon"
AddElement(lCountryCodes()):lCountryCodes() = "LC|St Lucia"
AddElement(lCountryCodes()):lCountryCodes() = "LI|Liechtenstein"
AddElement(lCountryCodes()):lCountryCodes() = "LK|Sri Lanka"
AddElement(lCountryCodes()):lCountryCodes() = "LR|Liberia"
AddElement(lCountryCodes()):lCountryCodes() = "LS|Lesotho"
AddElement(lCountryCodes()):lCountryCodes() = "LT|Lithuania"
AddElement(lCountryCodes()):lCountryCodes() = "LU|Luxembourg"
AddElement(lCountryCodes()):lCountryCodes() = "LV|Latvia"
AddElement(lCountryCodes()):lCountryCodes() = "LY|Libya"
AddElement(lCountryCodes()):lCountryCodes() = "MA|Morocco"
AddElement(lCountryCodes()):lCountryCodes() = "MC|Monaco"
AddElement(lCountryCodes()):lCountryCodes() = "MD|Moldova"
AddElement(lCountryCodes()):lCountryCodes() = "ME|Montenegro"
AddElement(lCountryCodes()):lCountryCodes() = "MF|St Martin"
AddElement(lCountryCodes()):lCountryCodes() = "MG|Madagascar"
AddElement(lCountryCodes()):lCountryCodes() = "MH|Marshall Islands"
AddElement(lCountryCodes()):lCountryCodes() = "MK|Macedonia, Former Yugoslav Republic of"
AddElement(lCountryCodes()):lCountryCodes() = "ML|Mali"
AddElement(lCountryCodes()):lCountryCodes() = "MM|Myanmar"
AddElement(lCountryCodes()):lCountryCodes() = "MN|Mongolia"
AddElement(lCountryCodes()):lCountryCodes() = "MO|Macau"
AddElement(lCountryCodes()):lCountryCodes() = "MP|Northern Mariana Islands"
AddElement(lCountryCodes()):lCountryCodes() = "MQ|Martinique"
AddElement(lCountryCodes()):lCountryCodes() = "MR|Mauritania"
AddElement(lCountryCodes()):lCountryCodes() = "MS|Montserrat"
AddElement(lCountryCodes()):lCountryCodes() = "MT|Malta"
AddElement(lCountryCodes()):lCountryCodes() = "MU|Mauritius"
AddElement(lCountryCodes()):lCountryCodes() = "MV|Maldives"
AddElement(lCountryCodes()):lCountryCodes() = "MW|Malawi"
AddElement(lCountryCodes()):lCountryCodes() = "MX|Mexico"
AddElement(lCountryCodes()):lCountryCodes() = "MY|Malaysia"
AddElement(lCountryCodes()):lCountryCodes() = "MZ|Mozambique"
AddElement(lCountryCodes()):lCountryCodes() = "NA|Namibia"
AddElement(lCountryCodes()):lCountryCodes() = "NC|New Caledonia"
AddElement(lCountryCodes()):lCountryCodes() = "NE|Niger"
AddElement(lCountryCodes()):lCountryCodes() = "NF|Norfolk Island"
AddElement(lCountryCodes()):lCountryCodes() = "NG|Nigeria"
AddElement(lCountryCodes()):lCountryCodes() = "NI|Nicaragua"
AddElement(lCountryCodes()):lCountryCodes() = "NL|Netherlands"
AddElement(lCountryCodes()):lCountryCodes() = "NO|Norway"
AddElement(lCountryCodes()):lCountryCodes() = "NP|Nepal"
AddElement(lCountryCodes()):lCountryCodes() = "NR|Nauru"
AddElement(lCountryCodes()):lCountryCodes() = "NU|Niue"
AddElement(lCountryCodes()):lCountryCodes() = "NZ|New Zealand"
AddElement(lCountryCodes()):lCountryCodes() = "OM|Oman"
AddElement(lCountryCodes()):lCountryCodes() = "OU|Carriacou"
AddElement(lCountryCodes()):lCountryCodes() = "PA|Panama"
AddElement(lCountryCodes()):lCountryCodes() = "PE|Peru"
AddElement(lCountryCodes()):lCountryCodes() = "PF|French Polynesia"
AddElement(lCountryCodes()):lCountryCodes() = "PG|Papua New Guinea"
AddElement(lCountryCodes()):lCountryCodes() = "PH|Philippines"
AddElement(lCountryCodes()):lCountryCodes() = "PK|Pakistan"
AddElement(lCountryCodes()):lCountryCodes() = "PL|Poland"
AddElement(lCountryCodes()):lCountryCodes() = "PM|St Pierre and Miquelon"
AddElement(lCountryCodes()):lCountryCodes() = "PN|Pitcairn Island"
AddElement(lCountryCodes()):lCountryCodes() = "PR|Puerto Rico"
AddElement(lCountryCodes()):lCountryCodes() = "PS|Palestinian Territories"
AddElement(lCountryCodes()):lCountryCodes() = "PT|Portugal"
AddElement(lCountryCodes()):lCountryCodes() = "PW|Palau"
AddElement(lCountryCodes()):lCountryCodes() = "PY|Paraguay"
AddElement(lCountryCodes()):lCountryCodes() = "QA|Qatar"
AddElement(lCountryCodes()):lCountryCodes() = "RE|Reunion"
AddElement(lCountryCodes()):lCountryCodes() = "RK|Kosovo"
AddElement(lCountryCodes()):lCountryCodes() = "RO|Romania"
AddElement(lCountryCodes()):lCountryCodes() = "RS|Serbia"
AddElement(lCountryCodes()):lCountryCodes() = "RU|Russia"
AddElement(lCountryCodes()):lCountryCodes() = "RW|Rwanda"
AddElement(lCountryCodes()):lCountryCodes() = "SA|Saudi Arabia"
AddElement(lCountryCodes()):lCountryCodes() = "SB|Solomon Islands"
AddElement(lCountryCodes()):lCountryCodes() = "SC|Seychelles"
AddElement(lCountryCodes()):lCountryCodes() = "SD|Sudan"
AddElement(lCountryCodes()):lCountryCodes() = "SE|Sweden"
AddElement(lCountryCodes()):lCountryCodes() = "SG|Singapore"
AddElement(lCountryCodes()):lCountryCodes() = "SH|St Helena"
AddElement(lCountryCodes()):lCountryCodes() = "SI|Slovenia"
AddElement(lCountryCodes()):lCountryCodes() = "SJ|Svalbard and Jan Meyen Islands"
AddElement(lCountryCodes()):lCountryCodes() = "SK|Slovakia"
AddElement(lCountryCodes()):lCountryCodes() = "SL|Sierra Leone"
AddElement(lCountryCodes()):lCountryCodes() = "SM|San Marino"
AddElement(lCountryCodes()):lCountryCodes() = "SN|Senegal"
AddElement(lCountryCodes()):lCountryCodes() = "SO|Somalia"
AddElement(lCountryCodes()):lCountryCodes() = "SR|Suriname"
AddElement(lCountryCodes()):lCountryCodes() = "SS|South Sudan"
AddElement(lCountryCodes()):lCountryCodes() = "ST|Sao Tome and Principe"
AddElement(lCountryCodes()):lCountryCodes() = "SV|El Salvador"
AddElement(lCountryCodes()):lCountryCodes() = "SX|Sint Maarten"
AddElement(lCountryCodes()):lCountryCodes() = "SY|Syria"
AddElement(lCountryCodes()):lCountryCodes() = "SZ|Swaziland"
AddElement(lCountryCodes()):lCountryCodes() = "TC|Turks and Caicos Islands"
AddElement(lCountryCodes()):lCountryCodes() = "TD|Chad"
AddElement(lCountryCodes()):lCountryCodes() = "TF|French Southern Territories"
AddElement(lCountryCodes()):lCountryCodes() = "TG|Togo"
AddElement(lCountryCodes()):lCountryCodes() = "TH|Thailand"
AddElement(lCountryCodes()):lCountryCodes() = "TJ|Tajikistan"
AddElement(lCountryCodes()):lCountryCodes() = "TK|Tokelau"
AddElement(lCountryCodes()):lCountryCodes() = "TM|Turkmenistan"
AddElement(lCountryCodes()):lCountryCodes() = "TN|Tunisia"
AddElement(lCountryCodes()):lCountryCodes() = "TO|Tonga"
AddElement(lCountryCodes()):lCountryCodes() = "TP|Timor-Leste"
AddElement(lCountryCodes()):lCountryCodes() = "TR|Turkey"
AddElement(lCountryCodes()):lCountryCodes() = "TT|Trinidad and Tobago"
AddElement(lCountryCodes()):lCountryCodes() = "TV|Tuvalu"
AddElement(lCountryCodes()):lCountryCodes() = "TW|Taiwan, China"
AddElement(lCountryCodes()):lCountryCodes() = "TZ|Tanzania"
AddElement(lCountryCodes()):lCountryCodes() = "UA|Ukraine"
AddElement(lCountryCodes()):lCountryCodes() = "UG|Uganda"
AddElement(lCountryCodes()):lCountryCodes() = "UM|United States Minor Outlying Islands"
AddElement(lCountryCodes()):lCountryCodes() = "US|United States"
AddElement(lCountryCodes()):lCountryCodes() = "UX|Unserviced Destn."
AddElement(lCountryCodes()):lCountryCodes() = "UY|Uruguay"
AddElement(lCountryCodes()):lCountryCodes() = "UZ|Uzbekistan"
AddElement(lCountryCodes()):lCountryCodes() = "VA|Vatican City State"
AddElement(lCountryCodes()):lCountryCodes() = "VC|St Vincent and The Grenadines"
AddElement(lCountryCodes()):lCountryCodes() = "VE|Venezuela"
AddElement(lCountryCodes()):lCountryCodes() = "VG|British Virgin Islands"
AddElement(lCountryCodes()):lCountryCodes() = "VI|US Virgin Islands"
AddElement(lCountryCodes()):lCountryCodes() = "VN|Vietnam"
AddElement(lCountryCodes()):lCountryCodes() = "VU|Vanuatu"
AddElement(lCountryCodes()):lCountryCodes() = "WF|Wallis and Futuna Islands"
AddElement(lCountryCodes()):lCountryCodes() = "WS|Samoa"
AddElement(lCountryCodes()):lCountryCodes() = "YE|Yemen"
AddElement(lCountryCodes()):lCountryCodes() = "YT|Mayotte"
AddElement(lCountryCodes()):lCountryCodes() = "ZA|South Africa"
AddElement(lCountryCodes()):lCountryCodes() = "ZM|Zambia"
AddElement(lCountryCodes()):lCountryCodes() = "ZW|Zimbabwe"

Procedure.s pISOCountry(isocountry.s, getiso.b=#False, showAll.b=#False)
  ResetList(lCountryCodes())
  While NextElement(lCountryCodes())
    s1$=StringField(lCountryCodes(),1,"|"):s2$=StringField(lCountryCodes(),2,"|")
    If getiso = #True
      If ExtractRegExString(s2$, "(?i).*"+isocountry+".*") 
        If showAll = #True
          mReturn$ + s1$ + " : " + s2$ + #LF$
        Else
          ProcedureReturn s1$
        EndIf
      EndIf
    Else     
      If showAll = #True
        If ExtractRegExString(s1$, "(?i)^"+isocountry+".*"):mReturn$ + s1$ + " : " + s2$ + #LF$:EndIf      
      Else   
        If isocountry = s1$:ProcedureReturn s2$:EndIf
      EndIf        
    EndIf
  Wend
  If mReturn$:ProcedureReturn Left(mReturn$,Len(mReturn$)-1):Else:ProcedureReturn "?":EndIf
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
;Examples
  Debug pISOCountry("FR") ; return country name
  Debug pISOCountry("F",#False,#True) ; return all ISOs matching
  Debug pISOCountry("France",#True) ; return ISO
  Debug pISOCountry("an",#True,#True) ; return all ISO/Countries matching
  testLoop$=pISOCountry("an",#True,#True) ; show test loop through each
  For i = 1 To CountString(testLoop$,#LF$) + 1
    Debug Str(i) + ": " + StringField(testLoop$,i,#LF$)
  Next
  Debug StringField(pISOCountry("an",#True,#True),1,#LF$) ; show only first match
CompilerEndIf
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Best way to put this country list into my program?

Post by infratec »

My solution:

Code: Select all


Global NewMap lCountryCodes.s()

lCountryCodes("AD") = "Andorra"
lCountryCodes("AE") = "United Arab Emirates"
lCountryCodes("AF") = "Afghanistan"
lCountryCodes("AG") = "Antigua and Barbuda"
lCountryCodes("AI") = "Anguilla"
lCountryCodes("AL") = "Albania"
lCountryCodes("AM") = "Armenia"
lCountryCodes("AN") = "Netherlands Antilles"
lCountryCodes("AO") = "Angola"
lCountryCodes("AQ") = "Antarctica"
lCountryCodes("AR") = "Argentina"
lCountryCodes("AS") = "American Samoa"
lCountryCodes("AT") = "Austria"
lCountryCodes("AU") = "Australia"
lCountryCodes("AW") = "Aruba"
lCountryCodes("AZ") = "Azerbaijan"
lCountryCodes("BA") = "Bosnia and Herzegovina"
lCountryCodes("BB") = "Barbados"
lCountryCodes("BD") = "Bangladesh"
lCountryCodes("BE") = "Belgium"
lCountryCodes("BF") = "Burkina Faso"
lCountryCodes("BG") = "Bulgaria"
lCountryCodes("BH") = "Bahrain"
lCountryCodes("BI") = "Burundi"
lCountryCodes("BJ") = "Benin"
lCountryCodes("BM") = "Bermuda"
lCountryCodes("BN") = "Brunei Darussalam"
lCountryCodes("BO") = "Bolivia"
lCountryCodes("BR") = "Brazil"
lCountryCodes("BS") = "Bahamas"
lCountryCodes("BT") = "Bhutan"
lCountryCodes("BV") = "Bouvet Island"
lCountryCodes("BW") = "Botswana"
lCountryCodes("BY") = "Belarus"
lCountryCodes("BZ") = "Belize"
lCountryCodes("CA") = "Canada"
lCountryCodes("CB") = "Scott Base"
lCountryCodes("CC") = "Cocos / Keeling Islands"
lCountryCodes("CD") = "Congo, Democratic Repulic of the"
lCountryCodes("CF") = "Central African Republic"
lCountryCodes("CG") = "Congo, Republic of the"
lCountryCodes("CH") = "Switzerland"
lCountryCodes("CI") = "Cote dIvoire (Ivory Coast)"
lCountryCodes("CK") = "Cook Islands"
lCountryCodes("CL") = "Chile"
lCountryCodes("CM") = "Cameroon"
lCountryCodes("CN") = "China"
lCountryCodes("CO") = "Colombia"
lCountryCodes("CR") = "Costa Rica"
lCountryCodes("CU") = "Cuba"
lCountryCodes("CV") = "Cape Verde"
lCountryCodes("CW") = "Curacao"
lCountryCodes("CX") = "Christmas Island"
lCountryCodes("CY") = "Cyprus"
lCountryCodes("CZ") = "Czech Republic"
lCountryCodes("DE") = "Germany"
lCountryCodes("DJ") = "Djibouti"
lCountryCodes("DK") = "Denmark"
lCountryCodes("DM") = "Dominica"
lCountryCodes("DO") = "Dominican Republic"
lCountryCodes("DZ") = "Algeria"
lCountryCodes("EC") = "Ecuador"
lCountryCodes("EE") = "Estonia"
lCountryCodes("EG") = "Egypt"
lCountryCodes("ER") = "Eritrea"
lCountryCodes("ES") = "Spain"
lCountryCodes("ET") = "Ethiopia"
lCountryCodes("FI") = "Finland"
lCountryCodes("FJ") = "Fiji"
lCountryCodes("FK") = "Falkland Islands"
lCountryCodes("FM") = "Micronesia"
lCountryCodes("FO") = "Faroe Islands"
lCountryCodes("FR") = "France"
lCountryCodes("GA") = "Gabon"
lCountryCodes("GB") = "United Kingdom"
lCountryCodes("GD") = "Grenada"
lCountryCodes("GE") = "Georgia"
lCountryCodes("GF") = "French Guiana"
lCountryCodes("GG") = "Guernsey"
lCountryCodes("GH") = "Ghana"
lCountryCodes("GI") = "Gibraltar"
lCountryCodes("GL") = "Greenland"
lCountryCodes("GM") = "Gambia"
lCountryCodes("GN") = "Guinea"
lCountryCodes("GP") = "Guadeloupe"
lCountryCodes("GQ") = "Equatorial Guinea"
lCountryCodes("GR") = "Greece"
lCountryCodes("GS") = "South Georgia"
lCountryCodes("GT") = "Guatemala"
lCountryCodes("GU") = "Guam"
lCountryCodes("GW") = "Guinea-Bissau"
lCountryCodes("GY") = "Guyana"
lCountryCodes("HK") = "Hong Kong, China"
lCountryCodes("HM") = "Heard and McDonald Islands"
lCountryCodes("HN") = "Honduras"
lCountryCodes("HR") = "Croatia"
lCountryCodes("HT") = "Haiti"
lCountryCodes("HU") = "Hungary"
lCountryCodes("ID") = "Indonesia"
lCountryCodes("IE") = "Ireland"
lCountryCodes("IL") = "Israel"
lCountryCodes("IM") = "Isle of Man"
lCountryCodes("IN") = "India"
lCountryCodes("IO") = "British Indian Ocean Territory"
lCountryCodes("IQ") = "Iraq"
lCountryCodes("IR") = "Iran"
lCountryCodes("IS") = "Iceland"
lCountryCodes("IT") = "Italy"
lCountryCodes("JE") = "Jersey"
lCountryCodes("JM") = "Jamaica"
lCountryCodes("JO") = "Jordan"
lCountryCodes("JP") = "Japan"
lCountryCodes("KA") = "Kerguelen Archipelago"
lCountryCodes("KE") = "Kenya"
lCountryCodes("KG") = "Kyrgyzstan"
lCountryCodes("KH") = "Cambodia"
lCountryCodes("KI") = "Kiribati"
lCountryCodes("KM") = "Comoros"
lCountryCodes("KN") = "St Kitts and Nevis"
lCountryCodes("KP") = "Korea, Democratic Peoples Republic of"
lCountryCodes("KR") = "Korea, Republic of"
lCountryCodes("KW") = "Kuwait"
lCountryCodes("KY") = "Cayman Islands"
lCountryCodes("KZ") = "Kazakhstan"
lCountryCodes("LA") = "Lao Peoples Democratic Republic"
lCountryCodes("LB") = "Lebanon"
lCountryCodes("LC") = "St Lucia"
lCountryCodes("LI") = "Liechtenstein"
lCountryCodes("LK") = "Sri Lanka"
lCountryCodes("LR") = "Liberia"
lCountryCodes("LS") = "Lesotho"
lCountryCodes("LT") = "Lithuania"
lCountryCodes("LU") = "Luxembourg"
lCountryCodes("LV") = "Latvia"
lCountryCodes("LY") = "Libya"
lCountryCodes("MA") = "Morocco"
lCountryCodes("MC") = "Monaco"
lCountryCodes("MD") = "Moldova"
lCountryCodes("ME") = "Montenegro"
lCountryCodes("MF") = "St Martin"
lCountryCodes("MG") = "Madagascar"
lCountryCodes("MH") = "Marshall Islands"
lCountryCodes("MK") = "Macedonia, Former Yugoslav Republic of"
lCountryCodes("ML") = "Mali"
lCountryCodes("MM") = "Myanmar"
lCountryCodes("MN") = "Mongolia"
lCountryCodes("MO") = "Macau"
lCountryCodes("MP") = "Northern Mariana Islands"
lCountryCodes("MQ") = "Martinique"
lCountryCodes("MR") = "Mauritania"
lCountryCodes("MS") = "Montserrat"
lCountryCodes("MT") = "Malta"
lCountryCodes("MU") = "Mauritius"
lCountryCodes("MV") = "Maldives"
lCountryCodes("MW") = "Malawi"
lCountryCodes("MX") = "Mexico"
lCountryCodes("MY") = "Malaysia"
lCountryCodes("MZ") = "Mozambique"
lCountryCodes("NA") = "Namibia"
lCountryCodes("NC") = "New Caledonia"
lCountryCodes("NE") = "Niger"
lCountryCodes("NF") = "Norfolk Island"
lCountryCodes("NG") = "Nigeria"
lCountryCodes("NI") = "Nicaragua"
lCountryCodes("NL") = "Netherlands"
lCountryCodes("NO") = "Norway"
lCountryCodes("NP") = "Nepal"
lCountryCodes("NR") = "Nauru"
lCountryCodes("NU") = "Niue"
lCountryCodes("NZ") = "New Zealand"
lCountryCodes("OM") = "Oman"
lCountryCodes("OU") = "Carriacou"
lCountryCodes("PA") = "Panama"
lCountryCodes("PE") = "Peru"
lCountryCodes("PF") = "French Polynesia"
lCountryCodes("PG") = "Papua New Guinea"
lCountryCodes("PH") = "Philippines"
lCountryCodes("PK") = "Pakistan"
lCountryCodes("PL") = "Poland"
lCountryCodes("PM") = "St Pierre and Miquelon"
lCountryCodes("PN") = "Pitcairn Island"
lCountryCodes("PR") = "Puerto Rico"
lCountryCodes("PS") = "Palestinian Territories"
lCountryCodes("PT") = "Portugal"
lCountryCodes("PW") = "Palau"
lCountryCodes("PY") = "Paraguay"
lCountryCodes("QA") = "Qatar"
lCountryCodes("RE") = "Reunion"
lCountryCodes("RK") = "Kosovo"
lCountryCodes("RO") = "Romania"
lCountryCodes("RS") = "Serbia"
lCountryCodes("RU") = "Russia"
lCountryCodes("RW") = "Rwanda"
lCountryCodes("SA") = "Saudi Arabia"
lCountryCodes("SB") = "Solomon Islands"
lCountryCodes("SC") = "Seychelles"
lCountryCodes("SD") = "Sudan"
lCountryCodes("SE") = "Sweden"
lCountryCodes("SG") = "Singapore"
lCountryCodes("SH") = "St Helena"
lCountryCodes("SI") = "Slovenia"
lCountryCodes("SJ") = "Svalbard and Jan Meyen Islands"
lCountryCodes("SK") = "Slovakia"
lCountryCodes("SL") = "Sierra Leone"
lCountryCodes("SM") = "San Marino"
lCountryCodes("SN") = "Senegal"
lCountryCodes("SO") = "Somalia"
lCountryCodes("SR") = "Suriname"
lCountryCodes("SS") = "South Sudan"
lCountryCodes("ST") = "Sao Tome and Principe"
lCountryCodes("SV") = "El Salvador"
lCountryCodes("SX") = "Sint Maarten"
lCountryCodes("SY") = "Syria"
lCountryCodes("SZ") = "Swaziland"
lCountryCodes("TC") = "Turks and Caicos Islands"
lCountryCodes("TD") = "Chad"
lCountryCodes("TF") = "French Southern Territories"
lCountryCodes("TG") = "Togo"
lCountryCodes("TH") = "Thailand"
lCountryCodes("TJ") = "Tajikistan"
lCountryCodes("TK") = "Tokelau"
lCountryCodes("TM") = "Turkmenistan"
lCountryCodes("TN") = "Tunisia"
lCountryCodes("TO") = "Tonga"
lCountryCodes("TP") = "Timor-Leste"
lCountryCodes("TR") = "Turkey"
lCountryCodes("TT") = "Trinidad and Tobago"
lCountryCodes("TV") = "Tuvalu"
lCountryCodes("TW") = "Taiwan, China"
lCountryCodes("TZ") = "Tanzania"
lCountryCodes("UA") = "Ukraine"
lCountryCodes("UG") = "Uganda"
lCountryCodes("UM") = "United States Minor Outlying Islands"
lCountryCodes("US") = "United States"
lCountryCodes("UX") = "Unserviced Destn."
lCountryCodes("UY") = "Uruguay"
lCountryCodes("UZ") = "Uzbekistan"
lCountryCodes("VA") = "Vatican City State"
lCountryCodes("VC") = "St Vincent and The Grenadines"
lCountryCodes("VE") = "Venezuela"
lCountryCodes("VG") = "British Virgin Islands"
lCountryCodes("VI") = "US Virgin Islands"
lCountryCodes("VN") = "Vietnam"
lCountryCodes("VU") = "Vanuatu"
lCountryCodes("WF") = "Wallis and Futuna Islands"
lCountryCodes("WS") = "Samoa"
lCountryCodes("YE") = "Yemen"
lCountryCodes("YT") = "Mayotte"
lCountryCodes("ZA") = "South Africa"
lCountryCodes("ZM") = "Zambia"
lCountryCodes("ZW") = "Zimbabwe"

Structure CountryCodeStructure
  ISO$
  Country$
EndStructure

Procedure.i pISOCountry(List ResultList.CountryCodeStructure(), isocountry.s, getiso.i=#False, showAll.i=#False)
  
  ClearList(ResultList())
  If getiso
    If showAll
      isocountry = LCase(isocountry)
      ForEach lCountryCodes()
        If FindString(LCase(lCountryCodes()), isocountry)
          AddElement(ResultList())
          ResultList()\ISO$ = MapKey(lCountryCodes())
          ResultList()\Country$ = lCountryCodes()
        EndIf
      Next
    Else
      ForEach lCountryCodes()
        If lCountryCodes() = isocountry
          AddElement(ResultList())
          ResultList()\ISO$ = MapKey(lCountryCodes())
          ResultList()\Country$ = lCountryCodes()
          Break
        EndIf
      Next
    EndIf
  Else
    If showAll
      ForEach lCountryCodes()
        If Left(MapKey(lCountryCodes()), 1) = isocountry
          AddElement(ResultList())
          ResultList()\ISO$ = MapKey(lCountryCodes())
          ResultList()\Country$ = lCountryCodes()
        EndIf
      Next
    Else
      If FindMapElement(lCountryCodes(), isocountry)
        AddElement(ResultList())
        ResultList()\ISO$ = MapKey(lCountryCodes())
        ResultList()\Country$ = lCountryCodes()
      EndIf
    EndIf
  EndIf
  
  SortStructuredList(ResultList(), #PB_Sort_Ascending, OffsetOf(CountryCodeStructure\ISO$), #PB_String)
  
  ProcedureReturn ListSize(ResultList())
  
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  
  Define i.i
  NewList ResultList.CountryCodeStructure()
  
  ;Examples
  If pISOCountry(ResultList(), "FR") ; return country name
    Debug ResultList()\Country$
  EndIf
  If pISOCountry(ResultList(), "F",#False,#True) ; return all ISOs matching
    ForEach ResultList()
      Debug ResultList()\ISO$ + " : " + ResultList()\Country$
    Next
  EndIf
  If pISOCountry(ResultList(), "France",#True) ; return ISO
    Debug ResultList()\ISO$
  EndIf
  If pISOCountry(ResultList(), "an",#True,#True) ; return all ISO/Countries matching
    ForEach ResultList()
      i + 1
      Debug Str(i) + ": " + ResultList()\ISO$ + " : " + ResultList()\Country$
    Next
  EndIf

  If pISOCountry(ResultList(), "an",#True,#True)
    FirstElement(ResultList())
    Debug ResultList()\ISO$ + " : " + ResultList()\Country$
  EndIf
CompilerEndIf
1. Faster since no regex and less stringfields
2. exe much smaller since regex lib is not needed
3. you get the found result count as return value
Post Reply