Best way to put this country list into my program?

Just starting out? Need help? Post your questions and find answers here.
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Best way to put this country list into my program?

Post by vwidmer »

Right now I have a file with a list of all the countries as such

Code: Select all

AD,Andorra
AE,United Arab Emirates
AF,Afghanistan
AG,Antigua and Barbuda
AI,Anguilla
AL,Albania
And I am using this code to read it into a list:

Code: Select all

If ReadFile(0, "myCountries.countries")
    While Eof(0) = 0
      AddElement(countryCodesF()):countryCodesF() = StringField(ReadString(0),1,",")
    Wend
    CloseFile(0) 
  Else
    MessageRequester("Information","Couldn't open the file!")
  EndIf
I would like to put them into the program so it doesnt have to have an extra file.
Any suggestions on best/easiest ways to do this?

Thanks
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
User avatar
Michael Vogel
Addict
Addict
Posts: 2810
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

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

Post by Michael Vogel »

Depending on how often you need to access the country data you'd put it within a DataSection as strings and read it a single time during the program start (into a array or list) or everytime when needed (using restore and read) to save some memory...
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 283
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

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

Post by oreopa »

CatchText wouldn't be a bad addition to PB.

Depending on how you need to use the data, using map seems a good fit.
Proud supporter of PB! * Musician * C64/6502 Freak
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

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

Post by idle »

as suggested maybe try a map, you can either add them explicitly or include them as a file in the Datasection

Code: Select all

Global NewMap mCountries.s() 

Procedure LoadCountries() 
  Protected num,*ptr,value.s  
  
  num = PeekL(?Countries)  
  *ptr = ?Countries + SizeOf(long) 
  
  For a = 0 To num 
    value = PeekS(*ptr)
    AddMapElement(mCountries(),StringField(value,1,",")) 
    mCountries() = StringField(value,2,",") 
    *ptr + StringByteLength(value)+SizeOf(Character) 
  Next   
    
  DataSection 
    Countries: 
    Data.l 5
    Data.s "AD,Andorra"
    Data.s "AE,United Arab Emirates"
    Data.s "AF,Afghanistan"
    Data.s "AG,Antigua And Barbuda"
    Data.s "AI,Anguilla"
    Data.s "AL,Albania" 
  EndDataSection   
  
EndProcedure 

LoadCountries()

ForEach mCountries() 
  Debug MapKey(mCountries()) + " - " + mCountries()  
Next 

Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

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

Post by kpeters58 »

Code: Select all

  NewMap CMap.s()
  ;  
  CMap("CA") = "Canada"
  CMap("GE") = "Germany"
  CMap("FR") = "France"
 

And to keep it clean, I'd put these 200+ lines (assuming you'll have all countries of the world) in a separate file like Countries.PBI
and

Code: Select all

XInclude Path_to_Countries_File + #PS$ + "Countries.PBI"
it in my code - to me, that's the most succinct and tidy way I can think of.
PB 5.73 on Windows 10 & OS X High Sierra
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

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

Post by Paul »

If you already have a text file and don't want to do any reformatting, you can use IncludeBinary,
then maybe something simple like this to parse it...

Code: Select all

countries$=ReplaceString(PeekS(?cstart,?cend-?cstart,#PB_Ascii),#CRLF$,",")
For tmp=1 To CountString(countries$,",")+1
  Debug StringField(countries$,tmp,",")
Next
End


DataSection
  cstart:
  IncludeBinary "countries.txt"
  cend:
EndDataSection
Image Image
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post by Little John »

Paul wrote:

Code: Select all

For tmp=1 To CountString(countries$,",")+1
   [...]
Next
Putting a function call after To in PureBasic is never a good idea, because that function is called again and again in each iteration of the loop. In contrast e.g. to PowerBasic, PureBasic does not store the function result in an internal variable. So we should store it in a variable ourselves:

Code: Select all

numFields = CountString(countries$,",") + 1
For tmp = 1 To numFields
   [...]
Next
Last edited by Little John on Sat May 04, 2019 11:12 am, edited 1 time in total.
User avatar
Michael Vogel
Addict
Addict
Posts: 2810
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

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

Post by Michael Vogel »

Still not clear if speed, memory consumption or anything else is important...

...for normal I would read the whole data into a list/array/map or only the long names, while the country IDs in a single constant #CountryIDs="AE.AF.AL.AM.AO.....", so a simple (FindString(#CountryIDs,ID+".")+2)/3 would get the country index easily.

Another very simple method would look like this:

Code: Select all

Structure CountryType
	ID.s
	Name.s
	Alternative.s
EndStructure

DataSection
	Countries:
	Data.s "AE", "United Arab Emirates","Vereinigte Arabische Emirate"
	Data.s "AF", "Afghanistan",""
	Data.s "AL", "Albania","Albanien"
	Data.s "AM", "Armenia","Armenien"
	Data.s "AO", "Angola",""
	Data.s "AR", "Argentina","Argentinien"
	Data.s "AT", "Austria","Österreich"
	Data.s "AU", "Australia","Australien"
	Data.s "AW", "Aruba",""
	Data.s "AZ", "Azerbaijan","Aserbaidschan"
	Data.s "BA", "Bosnia and Herzegovina","Bosnien und Herzegowina"
	Data.s "BB", "Barbados",""
	Data.s "BD", "Bangladesh","Bangladesch"
	Data.s "BE", "Belgium","Belgien"
	Data.s "BF", "Burkina Faso",""
	Data.s "BG", "Bulgaria","Bulgarien"
	Data.s "BH", "Bahrain",""
	Data.s "BI", "Burundi",""
	Data.s "BJ", "Benin",""
	Data.s "BN", "Brunei",""
	Data.s "BO", "Bolivia","Bolivien"
	Data.s "BR", "Brazil","Brasilien"
	Data.s "BS", "Bahamas",""
	Data.s "BT", "Bhutan",""
	Data.s "BW", "Botswana",""
	Data.s "BY", "Belarus","Weißrussland"
	Data.s "BZ", "Belize",""
	Data.s "CA", "Canada","Kanada"
	Data.s "CD", "Democratic Republic of the Congo","Demokratische Republik Kongo"
	Data.s "CF", "Central African Republic","Zentralafrikanische Republik"
	Data.s "CG", "Republic of the Congo","Republik Kongo"
	Data.s "CH", "Switzerland","Schweiz"
	Data.s "CI", "Ivory Coast","Elfenbeinküste"
	Data.s "CL", "Chile",""
	Data.s "CM", "Cameroon","Kamerun"
	Data.s "CN", "China","China (Volksrepublik)"
	Data.s "CO", "Colombia","Kolumbien"
	Data.s "CR", "Costa Rica",""
	Data.s "CU", "Cuba","Kuba"
	Data.s "CV", "Cape Verde","Kap Verde"
	Data.s "CW", "Curacao","Curaçao"
	Data.s "CY", "Cyprus","Zypern"
	Data.s "CZ", "Czech Republic","Tschechien"
	Data.s "DE", "Germany","Deutschland"
	Data.s "DJ", "Djibouti","Dschibuti"
	Data.s "DK", "Denmark","Dänemark"
	Data.s "DO", "Dominican Republic","Dominikanische Republik"
	Data.s "DZ", "Algeria","Algerien"
	Data.s "EC", "Ecuador",""
	Data.s "EE", "Estonia","Estland"
	Data.s "EG", "Egypt","Ägypten"
	Data.s "EH", "Western Sahara","Demokratische Arabische Republik Sahara"
	Data.s "ER", "Eritrea",""
	Data.s "ES", "Spain","Spanien"
	Data.s "ET", "Ethiopia","Äthiopien"
	Data.s "FI", "Finland","Finnland"
	Data.s "FJ", "Fiji","Fidschi"
	Data.s "FR", "France","Frankreich"
	Data.s "GA", "Gabon","Gabun"
	Data.s "GB", "United Kingdom","Vereinigtes Königreich"
	Data.s "GE", "Georgia","Georgien"
	Data.s "GF", "French Guiana","Französisch Guyana"
	Data.s "GH", "Ghana",""
	Data.s "GI", "Gibraltar",""
	Data.s "GM", "Gambia",""
	Data.s "GN", "Guinea",""
	Data.s "GP", "Guadeloupe",""
	Data.s "GQ", "Equatorial Guinea","Äquatorialguinea"
	Data.s "GR", "Greece","Griechenland"
	Data.s "GT", "Guatemala",""
	Data.s "GU", "Guam",""
	Data.s "GW", "Guinea-Bissau",""
	Data.s "GY", "Guyana",""
	Data.s "HK", "Hong Kong","Hongkong"
	Data.s "HN", "Honduras",""
	Data.s "HR", "Croatia","Kroatien"
	Data.s "HT", "Haiti",""
	Data.s "HU", "Hungary","Ungarn"
	Data.s "ID", "Indonesia","Indonesien"
	Data.s "IE", "Ireland","Irland"
	Data.s "IL", "Israel",""
	Data.s "IM", "Isle of Man","Insel Man"
	Data.s "IN", "India","Indien"
	Data.s "IQ", "Iraq","Irak"
	Data.s "IR", "Iran",""
	Data.s "IS", "Iceland","Island"
	Data.s "IT", "Italy","Italien"
	Data.s "JE", "Jersey",""
	Data.s "JM", "Jamaica","Jamaika"
	Data.s "JO", "Jordan","Jordanien"
	Data.s "JP", "Japan",""
	Data.s "KE", "Kenya","Kenia"
	Data.s "KG", "Kyrgyzstan","Kirgisistan"
	Data.s "KH", "Cambodia","Kambodscha"
	Data.s "KI", "Kiribati",""
	Data.s "KM", "Comoros","Komoren"
	Data.s "KP", "North Korea","Nordkorea"
	Data.s "KR", "South Korea","Südkorea"
	Data.s "XK", "Kosovo",""
	Data.s "KW", "Kuwait",""
	Data.s "KY", "Cayman Islands","Kaiman Inseln"
	Data.s "KZ", "Kazakhstan","Kasachstan"
	Data.s "LA", "Laos",""
	Data.s "LB", "Lebanon","Libanon"
	Data.s "LK", "Sri Lanka",""
	Data.s "LR", "Liberia",""
	Data.s "LS", "Lesotho",""
	Data.s "LT", "Lithuania","Litauen"
	Data.s "LU", "Luxembourg","Luxemburg"
	Data.s "LV", "Latvia","Lettland"
	Data.s "LY", "Libya","Libyen"
	Data.s "MA", "Morocco","Marokko"
	Data.s "MC", "Monaco",""
	Data.s "MD", "Moldova","Moldawien"
	Data.s "ME", "Montenegro",""
	Data.s "MG", "Madagascar","Madagaskar"
	Data.s "MH", "Marshall Islands","Marshallinseln"
	Data.s "MK", "Macedonia","Mazedonien"
	Data.s "ML", "Mali",""
	Data.s "MM", "Myanmar",""
	Data.s "MN", "Mongolia","Mongolei"
	Data.s "MO", "Macao",""
	Data.s "MP", "Northern Mariana Islands","Marianen"
	Data.s "MQ", "Martinique",""
	Data.s "MR", "Mauritania","Mauretanien"
	Data.s "MU", "Mauritius",""
	Data.s "MV", "Maldives","Malediven"
	Data.s "MW", "Malawi",""
	Data.s "MX", "Mexico","Mexiko"
	Data.s "MY", "Malaysia",""
	Data.s "MZ", "Mozambique","Mosambik"
	Data.s "NA", "Namibia",""
	Data.s "NC", "New Caledonia","Neukaledonien"
	Data.s "NE", "Niger",""
	Data.s "NG", "Nigeria",""
	Data.s "NI", "Nicaragua",""
	Data.s "NL", "Netherlands","Niederlande"
	Data.s "NO", "Norway","Norwegen"
	Data.s "NP", "Nepal",""
	Data.s "NZ", "New Zealand","Neuseeland"
	Data.s "OM", "Oman",""
	Data.s "PA", "Panama",""
	Data.s "PE", "Peru",""
	Data.s "PF", "French Polynesia","Französisch Polynesien"
	Data.s "PG", "Papua New Guinea","Papua-Neuguinea"
	Data.s "PH", "Philippines","Philippinen"
	Data.s "PK", "Pakistan",""
	Data.s "PL", "Poland","Polen"
	Data.s "PR", "Puerto Rico",""
	Data.s "PS", "Palestinian Territory","Palästinensische Autonomiegebiete/Gazastreifen"
	Data.s "PT", "Portugal",""
	Data.s "PY", "Paraguay",""
	Data.s "QA", "Qatar","Katar"
	Data.s "RE", "Reunion",""
	Data.s "RO", "Romania","Rumänien"
	Data.s "RS", "Serbia","Serbien"
	Data.s "RU", "Russia","Russland"
	Data.s "RW", "Rwanda","Ruanda"
	Data.s "SA", "Saudi Arabia","Saudi-Arabien"
	Data.s "SB", "Solomon Islands","Salomonen"
	Data.s "SD", "Sudan",""
	Data.s "SS", "South Sudan","Südsudan"
	Data.s "SE", "Sweden","Schweden"
	Data.s "SG", "Singapore","Singapur"
	Data.s "SI", "Slovenia","Slowenien"
	Data.s "SK", "Slovakia","Slowakei"
	Data.s "SL", "Sierra Leone",""
	Data.s "SN", "Senegal",""
	Data.s "SO", "Somalia",""
	Data.s "SR", "Suriname",""
	Data.s "ST", "Sao Tome and Principe","São Tomé und Príncipe"
	Data.s "SV", "El Salvador",""
	Data.s "SY", "Syria","Syrien"
	Data.s "SZ", "Swaziland","Swasiland"
	Data.s "TD", "Chad","Tschad"
	Data.s "TG", "Togo",""
	Data.s "TH", "Thailand",""
	Data.s "TJ", "Tajikistan","Tadschikistan"
	Data.s "TL", "East Timor","Osttimor"
	Data.s "TM", "Turkmenistan",""
	Data.s "TN", "Tunisia","Tunesien"
	Data.s "TR", "Turkey","Türkei"
	Data.s "TT", "Trinidad and Tobago","Trinidad und Tobago"
	Data.s "TW", "Taiwan","Republik China (Taiwan)"
	Data.s "TZ", "Tanzania","Tansania"
	Data.s "UA", "Ukraine",""
	Data.s "UG", "Uganda",""
	Data.s "US", "United States","Vereinigte Staaten von Amerika"
	Data.s "UY", "Uruguay",""
	Data.s "UZ", "Uzbekistan","Usbekistan"
	Data.s "VE", "Venezuela",""
	Data.s "VI", "U.S. Virgin Islands","Virgin Island (USA)"
	Data.s "VN", "Vietnam",""
	Data.s "VU", "Vanuatu",""
	Data.s "WS", "Samoa",""
	Data.s "YE", "Yemen","Jemen"
	Data.s "YT", "Mayotte",""
	Data.s "ZA", "South Africa","Südafrika"
	Data.s "ZM", "Zambia","Sambia"
	Data.s "ZW", "Zimbabwe","Simbabwe"
EndDataSection

Procedure.s GetCountry(SearchID.s,PreferAlternative=#False)
	
	Protected i
	
	Restore Countries:
	Country.CountryType
	For i=1 To 200
		With Country
			Read.s \ID
			Read.s \Name
			Read.s \Alternative
			If \ID=SearchID
				If PreferAlternative And \Alternative
					ProcedureReturn \Alternative
				Else
					ProcedureReturn \Name
				EndIf
			EndIf
		EndWith
	Next i
	
	ProcedureReturn "?"
	
EndProcedure

Debug GetCountry("US")
Debug GetCountry("US",#True)
Debug GetCountry("WS",#True)
Debug GetCountry("XX",#True)
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 »

Heres my CountryList module :)

Its to big to post it in one go so just copy over the missing data from the
included pastebin link.

Features:
- list from ISO_3166-1 (2019) (feat. alpha2/ alpha3/ numeric)
- comes with country images (flags)
- sort & search supported

Code:

Code: Select all

DeclareModule WC
  Declare.i Init()
  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 FlagHandle(*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
  
  ;Country List by Mijikai
  ;Image Source: https://en.wikipedia.org/wiki/ISO_3166-1
  
  EnableExplicit
  
  UsePNGImageDecoder()
  UseBriefLZPacker()
  
  Structure COUNTRY_STRUCT
    name.s{128}
    alpha2.s{2}
    alpha3.s{3}
    numeric.w
    flagsize.w
    offset.l
  EndStructure
  
  Structure WC_STRUCT
    name.s{128}
    alpha2.s{2}
    alpha3.s{3}
    numeric.w
    flagsize.w
    offset.l
    id.i
    flag.i
    flag_handle.i
  EndStructure
  
  Global NewList wc.WC_STRUCT()
  Global *raw
  
  Procedure.i Init()
    Protected *dummy.COUNTRY_STRUCT
    Protected *size.Integer
    Protected *size_compressed.Integer
    Protected *img
    Protected index.i
    If Not *raw
      *size = ?wc
      *size_compressed = ?wc + 8
      *raw = AllocateMemory(*size\i)
      If *raw
        If UncompressMemory(?wc + 16,*size_compressed\i,*raw,*size\i) = *size\i
          *size = *raw
          *dummy = *size + 8
          For index = 0 To *size\i - 1
            *img = *dummy + SizeOf(COUNTRY_STRUCT)
            If AddElement(wc())
              CopyMemory(*dummy,@wc(),SizeOf(COUNTRY_STRUCT))
              wc()\id = index
              wc()\flag = CatchImage(#PB_Any,*img,*dummy\flagsize)
              If IsImage(wc()\flag)
                wc()\flag_handle = ImageID(wc()\flag)
              Else
                ClearList(wc())
                FreeMemory(*raw)
                *raw = #Null
                Break
              EndIf 
            Else
              ClearList(wc())
              FreeMemory(*raw)
              *raw = #Null
              Break
            EndIf
            *dummy + *dummy\offset
          Next
        Else
          FreeMemory(*raw)
          *raw = #Null
        EndIf 
      EndIf 
    EndIf
    If *raw
      FreeMemory(*raw)
    EndIf
    ProcedureReturn *raw
  EndProcedure
  
  Procedure.i SortABC()
    SortStructuredList(wc(),#PB_Sort_Ascending,OffsetOf(WC_STRUCT\id),#PB_Integer)
  EndProcedure
  
  Procedure.i SortNumeric()
    SortStructuredList(wc(),#PB_Sort_Ascending,OffsetOf(WC_STRUCT\numeric),#PB_Word)
  EndProcedure
  
  Procedure.i StartQuery()
    ResetList(wc())
  EndProcedure
  
  Procedure.i NextCountry()
    ProcedureReturn NextElement(wc())
  EndProcedure
  
  Procedure.s Name(*Country.WC_STRUCT)
    If *Country
      ProcedureReturn *Country\name
    EndIf 
  EndProcedure
  
  Procedure.s Alpha2(*Country.WC_STRUCT)
    If *Country
      ProcedureReturn *Country\alpha2
    EndIf 
  EndProcedure
  
  Procedure.s Alpha3(*Country.WC_STRUCT)
    If *Country
      ProcedureReturn *Country\alpha3
    EndIf 
  EndProcedure
  
  Procedure.i Numeric(*Country.WC_STRUCT)
    If *Country
      ProcedureReturn *Country\numeric
    EndIf 
  EndProcedure
  
  Procedure.i Flag(*Country.WC_STRUCT)
    If *Country
      ProcedureReturn *Country\flag
    EndIf 
  EndProcedure
  
  Procedure.i FlagHandle(*Country.WC_STRUCT)
    If *Country
      ProcedureReturn *Country\flag_handle
    EndIf 
  EndProcedure
  
  Procedure.i FindCountry(Name.s)
    ForEach wc()
      If FindString(wc()\name,Name)
        ProcedureReturn @wc()
      EndIf  
    Next
  EndProcedure
  
  Procedure.i FindAlpha2(Alpha.s)
    ForEach wc()
      If wc()\alpha2 = UCase(Alpha)
        ProcedureReturn @wc()
      EndIf  
    Next
  EndProcedure
  
  Procedure.i FindAlpha3(Alpha.s)
    ForEach wc()
      If wc()\alpha3 = UCase(Alpha)
        ProcedureReturn @wc()
      EndIf  
    Next
  EndProcedure
  
  Procedure.i FindNumeric(Value.i)
    ForEach wc()
      If wc()\numeric = Value
        ProcedureReturn @wc()
      EndIf  
    Next
  EndProcedure
  
  Procedure.i Release()
    ForEach wc()
      FreeImage(wc()\flag)
    Next
    ClearList(wc())
    *raw = #Null
  EndProcedure
  
  DataSection
    ;{ WC - WORLD COUNTRY LIST AND ICONS / ISO 3166-1 / YEAR 2019 }
    wc:
    !dq 180486
    !dq 74144
    
    ;-> COPY AND PASTE DATA FROM: https://pastebin.com/uCnmgk8K
    ;AND INSERT HERE:
    
    ;}
  EndDataSection
  
EndModule

EnableExplicit

Global *Country

If WC::Init()
  
  WC::StartQuery()
  WC::SortNumeric();<- sort by country numeric id
  
  Repeat
    *Country = WC::NextCountry()
    If *Country
      Debug WC::Name(*Country)
      Debug WC::Numeric(*Country)
    EndIf 
  Until *Country = #Null
  
  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 

Delay(5000)

End
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 »

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.
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: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...
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

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

Post by bosker »

You said you wanted the names embedded in the program and I would do that (as others suggest) by putting the data into the DataSection.

The minimum storage solution is to use the data directly from the DataSection.
How easy access will be depends on how you need to use the data.

Here's an example of how I use this technique to store and access data records (adapted for your country data).
The Country macro is just there to make setup easier. The 0,0 is list terminator.

Code: Select all

EnableExplicit

; Data structure in memory
Structure TCData
    CCode.s
    CName.s
EndStructure

; Array of countries data
Structure TCountries
    CData.TCData[0]
EndStructure

; For setting up data in datasection
Macro Country(Code, Name)
    Data.i @Code
    Data.i @Name
EndMacro


DataSection
LData:
    Country("AD", "Andorra")
    Country("AE", "United Arab Emirates")
    Country("AF", "Afghanistan")
    Country("AG", "Antigua and Barbuda")
    Country("AI", "Anguilla")
    Country("AL", "Albania")
    Data.i  0,0
EndDataSection

Define *CList.TCountries = ?LData
Define i

OpenConsole()

; Example: access whole list in sequence
While *CList\CData[i]\CCode
    Print(*CList\CData[i]\CCode + " ")
    PrintN(*CList\CData[i]\CName)
    i+1
WEnd
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 »

Thank you every one for the many ideas.

I will try them and see which works best for my app.

Thanks
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
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 »

vwidmer wrote:Right now I have a file with a list of all the countries as such

Code: Select all

AD,Andorra
AE,United Arab Emirates
AF,Afghanistan
AG,Antigua and Barbuda
AI,Anguilla
AL,Albania
I would like to put them into the program so it doesnt have to have an extra file.
As Paul had suggested (in the sixth post above), the best approach would be to use the IncludeBinary function. That way, the country codes text file will be integrated directly into the program binary itself, removing the need for any external files.

Here's a working example:

Code: Select all

; the full countries list text file can be viewed/downloaded from:
; https://www.dropbox.com/s/6nwhqnoft7wz71m/countriesList.txt?dl=0

Procedure getCountryCodes(List countryCodes.s(), List countryNames.s())
  
  countryData.s = PeekS(?cListStart, ?cListEnd - ?cListStart, #PB_Ascii)
  countryListCount = CountString(countryData, #CRLF$) + 1
  
  For i = 1 To countryListCount      
    
    countryNames$ = StringField(countryData, i, #CRLF$)    
    AddElement(countryNames())     
    countryNames() = countryNames$
    
    countryCode$ = StringField(countryNames$, 1, ",")    
    AddElement(countryCodes())     
    countryCodes() = countryCode$
    
  Next i
  
  DataSection
  cListStart:
    IncludeBinary "countriesList.txt"
  cListEnd:
  EndDataSection
  
EndProcedure

NewList countryCodes.s() : NewList countryNames.s() 
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 530, 600, "Country Codes", wFlags)
TextGadget(0, 10, 10, 100, 20, "Codes")
TextGadget(1, 120, 10, 400, 20, "Country Names (sorted by code)")
ListViewGadget(2, 10, 30, 100, 560)
ListViewGadget(3, 120, 30, 400, 560)

getCountryCodes(countryCodes(), countryNames())
SortList(countryCodes(), #PB_Sort_Ascending)
SortList(countryNames(), #PB_Sort_Ascending)
countryCodesListSize = ListSize(countryCodes())

FirstElement(countryCodes())
FirstElement(countryNames())

For i = 1 To countryCodesListSize
  
  AddGadgetItem(2, -1, countryCodes())
  NextElement(countryCodes())
  
  AddGadgetItem(3, -1, countryNames())  
  NextElement(countryNames())
  
Next i

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Once compiled, the program would run independently, without the need for the country codes text file.
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
oreopa
Enthusiast
Enthusiast
Posts: 283
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

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

Post by oreopa »

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.
LOL :D
Proud supporter of PB! * Musician * C64/6502 Freak
Post Reply