#!/usr/bin/ruby country_str = "294000|Iraq 659499|Afghanistan 295109|Republic of Macedonia 294471|Serbia 635648|Montenegro 635741|Reunion Island 635543|Panama 635541|Guatemala 635539|El Salvador 635533|Costa Rica 635531|Belize 635530|Central America 274952|Estonia 274960|Latvia 482863|Keyna 293774|Cape Verde 293762|Angola 293759|Zimbabwe 293778|Chad 612393|El Salvador 293788|Eritrea 293794|Gambia 190372|Cyprus 551471|Nicaragua 551470|Central America 295117|East Timor 60670|Northern Mariana Islands 528905|United States 528744|Nicaragua 274684|Czech Republic 191|United States 274881|Hungary 294139|Solomon Islands 294129|New Caledonia 294137|Samoa 294121|Kiribati 294338|French Polynesia 294115|Papua New Guinea 294131|Niue 294135|Palau 294141|Tonga 294143|Vanuatu 294198|Federated States of Micronesia 294328|Cook Islands 294331|Fiji 295113|Cocos (Keeling) Islands 295119|Norfolk Island 255104|New Zealand 255055|Australia 294481|Tuvalu 153339|Canada 150768|Mexico 147237|Caribbean 186216|United Kingdom 186591|Ireland 187070|France 187275|Germany 187427|Spain 187768|Italy 188045|Switzerland 188553|The Netherlands 188634|Belgium 189100|Portugal 189398|Greece 189512|Denmark 189806|Sweden 189896|Finland 189952|Iceland 190311|Malta 190329|Faroe Islands 190340|Luxembourg 190391|Andorra 190405|Monaco 190410|Austria 190455|Norway 301392|Marshall Islands 190357|Liechtenstein 580111|Central America 580112|Nicaragua 293717|Algeria 293730|Morocco 293738|Seychelles 293740|South Africa 293747|Tanzania 293753|Tunisia 293766|Botswana 293764|Benin 293768|Burkina Faso 293770|Burundi 293776|Central African Republic 293772|Cameroon 293786|Djibouti 293790|Ethiopia 293792|Gabon 293806|Libya 293796|Ghana 293800|Guinea-Bissau 293798|Guinea 293802|Lesotho 293812|Mali 293818|Mozambique 293828|Rwanda 293804|Liberia 293808|Madagascar 293810|Malawi 293814|Mauritania 293820|Namibia 293830|Senegal 293832|Sierra Leone 293816|Mauritius 293822|Niger 293824|Nigeria 293840|Uganda 293836|Swaziland 293834|Sudan 293838|Togo 293844|Bhutan 293842|Zambia 293889|Nepal 293910|Taiwan 293915|Thailand 293921|Vietnam 293935|Bangladesh 293929|Afghanistan 293931|Armenia 293965|Turkmenistan 293933|Azerbaijan 293937|Brunei Darussalam 293951|Malaysia 293939|Cambodia 293943|Kazakhstan 293947|Kyrgyzstan 293953|Maldives 293967|Uzbekistan 293949|Laos 293955|Mongolia 293959|Pakistan 293961|Sri Lanka 293963|Tajikistan 293969|Turkey 293977|Israel 293985|Jordan 294079|Paraguay 294010|Syria 294012|United Arab Emirates 293991|Saudi Arabia 293996|Bahrain 294008|Qatar 294081|Suriname 294071|Bolivia 294064|Uruguay 295105|Saint Helena 295116|Mayotte 295111|Greenland 295118|Western Sahara 294443|North Korea 294435|Comoros 294437|Equatorial Guinea 294439|Somalia 294441|Sao Tome and Principe 294459|Russia 294451|Bulgaria 294473|Ukraine 60665|American Samoa 60668|Guam 294186|Democratic Republic of the Congo 294188|Republic of the Congo 294190|Myanmar 294200|Egypt 294262|Singapore 294194|Georgia 294196|South Korea 294192|Cote d'Ivoire 294206|Kenya 294225|Indonesia 294266|Argentina 294232|Japan 294311|Peru 294291|Chile 294280|Brazil 294307|Ecuador 291982|Costa Rica 480228|Reunion 274947|Lithuania 274723|Poland 274922|Slovakia 274862|Slovenia 60862|Marshall Islands 294324|Venezuela 294077|Guyana 294075|French Guiana 294073|Colombia 612395|Guatemala 612408|Panama 612392|Central America 293998|Iran 294004|Lebanon 294014|Yemen 294006|Oman 294002|Kuwait 294447|Belarus 294449|Bosnia and Herzegovina 294453|Croatia 294445|Albania 294455|Moldova 294457|Romania 294211|China 293860|India 294245|Philippines 292002|Guatemala 292016|Honduras 294475|El Salvador 294477|Nicaragua 294479|Panama 291959|Belize 294270|Falkland Islands 503810|Upper Slaughter 503814|Lower Slaughter 612402|Nicaragua 608631|Central America 608637|El Salvador 608641|Guatemala 562624|Guatemala 608645|Panama 608632|Costa Rica 562623|Central America" i_c_hash = {} c_i_hash = {} country_str.split("\n").each do |s| k,v = s.strip.split("|") k = k.strip.to_i v = v.strip.downcase i_c_hash[k] = v c_i_hash[v] = k end arg = ARGV[0] #if we're looking up a code, echo the associated country if arg.to_i != 0 puts i_c_hash[arg.to_i] exit(0) end arg = arg.downcase #otherwise, first try an exact match on the name of the country if found = c_i_hash[arg] puts "#{arg} (#{found})" exit(0) end #otherwise, try for a partial match on the country names found = {} re = /#{arg}/ c_i_hash.each do |k, v| if k =~ re found[k] = v end end found.each do |k, v| puts "#{k} (#{v})" end