Trouble using bsearch with an array of strings - c

I am getting some confusing behaviour trying to use the c builtin bsearch on an array of strings in C. Here is the code. I know you can use the builtin strcmp for searching arrays of strings, but I included myStrCmp for debugging purposes because I didn't know why it wasn't working.
const char *stateNames[] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "Washington DC", "West Virginia", "Wisconsin", "Wyoming"};
int myStrCmp(const void *s1, const void *s2) {
printf("myStrCmp: s1(%p): %s, s2(%p): %s\n", s1, (char *)s1, s2, (char *)s2);
return strcmp(s1, s2);
}
int determineState(char *state) {
printf("state: %s\n", state);
for(int i = 0; i < 51; i++)
printf("stateNames[%i](%p): %s\n", i, &(stateNames[i]), stateNames[i]);
char *found = (char *) bsearch(state, stateNames, 51, sizeof(char *), myStrCmp );
if(found == NULL)
return -1;
return 0;
}
and here is some of the output when this function is called to look for Alabama.
stateNames[0](0x618440): Alabama
stateNames[1](0x618448): Alaska
stateNames[2](0x618450): Arizona
...
stateNames[24](0x618500): Missouri
stateNames[25](0x618508): Montana
stateNames[26](0x618510): Nebraska
stateNames[27](0x618518): Nevada
stateNames[28](0x618520): New Hampshire
stateNames[29](0x618528): New Jersey
stateNames[30](0x618530): New Mexico
stateNames[31](0x618538): New York
stateNames[32](0x618540): North Carolina
stateNames[33](0x618548): North Dakota
stateNames[34](0x618550): Ohio
stateNames[35](0x618558): Oklahoma
stateNames[36](0x618560): Oregon
stateNames[37](0x618568): Pennsylvania
stateNames[38](0x618570): Rhode Island
stateNames[39](0x618578): South Carolina
stateNames[40](0x618580): South Dakota
stateNames[41](0x618588): Tennessee
stateNames[42](0x618590): Texas
stateNames[43](0x618598): Utah
stateNames[44](0x6185a0): Vermont
stateNames[45](0x6185a8): Virginia
stateNames[46](0x6185b0): Washington
stateNames[47](0x6185b8): Washington DC
stateNames[48](0x6185c0): West Virginia
stateNames[49](0x6185c8): Wisconsin
stateNames[50](0x6185d0): Wyoming
myStrCmp: s1(0x415430): Alabama, s2(0x618508):
UA
myStrCmp: s1(0x415430): Alabama, s2(0x618570): A
myStrCmp: s1(0x415430): Alabama, s2(0x618540): PUA
myStrCmp: s1(0x415430): Alabama, s2(0x618528): 1UA
myStrCmp: s1(0x415430): Alabama, s2(0x618538): GUA
myStrCmp: s1(0x415430): Alabama, s2(0x618530): <UA
As you can see, the locations visited by bsearch in the course of its search should have valid strings (as was just checked before calling bsearch), but the output if you try to print the char * at that location is garbage. Can anyone see my mistake? Incidentally I get the same bad behaviour (but don't get to follow it as closely obviously) when I call bsearch with the final parameter set to:
(int(*)(const void*, const void*))strcmp
Thanks!

Since you are using an array of const char *, bsearch() will pass to the comparison function a pointer to those elements. In other words, it will receive const char * const * in its second argument.
int myStrCmp(const void *s1, const void *s2) {
const char *key = s1;
const char * const *arg = s2;
printf("myStrCmp: s1(%p): %s, s2(%p): %s\n", s1, key, s2, *arg);
return strcmp(key, *arg);
}

Your state name (or key) needs to be a pointer to a pointer. Didn't have to add/remove constanywhere. myStrCmpneeds to dereference by one to compare the strings. The code below does what you want I think. Please let me know if not, thanks.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *stateNames[] = {"Alabama", "Alaska", "Arizona", "Arkansas","California", "Colorado", "Connecticut", "Delaware", "Florida","Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "Washington DC", "West Virginia", "Wisconsin", "Wyoming"};
int myStrCmp(const void *s1, const void *s2) {
printf("myStrCmp: s1(%p): %s, s2(%p): %s\n", s1, *(char **)s1, s2, *(char**)s2);
return strcmp(*(char **) s1, *(char **) s2);
}
int determineState(char *state) {
printf("state: %s\n", state);
for(int i = 0; i < 51; i++)
printf("stateNames[%i](%p): %s\n", i, &(stateNames[i]), stateNames[i]);
char **found = (char **) bsearch(&state, stateNames, 51, sizeof(char *), myStrCmp );
if(found == NULL){
return -1;
} else {
printf("Found it!: %s\n", *found);
}
return 0;
}
int main(int argc, const char * argv[]) {
determineState("Alabama");
}

Related

How do you store a JSON Object like this in a React Hook UseState?

{
'New-York': [
"New York",
"Buffalo",
"Rochester",
"Yonkers",
"Syracuse",
"Albany",
"New Rochelle",
"Mount Vernon",
"Schenectady",
"Utica",
"White Plains",
"Hempstead",
"Troy",
"Niagara Falls",
"Binghamton",
"Freeport",
"Valley Stream"
],"California": [
"Los Angeles",
"San Diego",
"San Jose",
"San Francisco",
"Fresno",
"Sacramento",
"Long Beach",
"Oakland",
"Bakersfield",
"Anaheim",
"Santa Ana",
"Riverside",
"Stockton",
"Chula Vista",
"Irvine",
"Fremont",
"San Bernardino",
"Modesto",
"Fontana",
"Oxnard",
"Moreno Valley",
"Huntington Beach",
"Glendale",
"Santa Clarita",
"Garden Grove",
"Oceanside",
"Rancho Cucamonga",
"Santa Rosa",
"Ontario",
"Lancaster",
"Elk Grove",
"Corona",
"Palmdale",
"Salinas",
"Pomona",
"Hayward",
"Escondido",
"Torrance",
"Sunnyvale",
"Orange",
"Fullerton",
"Pasadena",
"Thousand Oaks",
"Visalia",
"Simi Valley",
"Concord",
"Roseville",
"Victorville",
"Santa Clara",
"Vallejo",
"Berkeley",
"El Monte",
"Downey",
"Costa Mesa",
"Inglewood",
"Carlsbad",
"San Buenaventura (Ventura)",
"Fairfield",
"West Covina",
"Murrieta",
"Richmond",
"Norwalk",
"Antioch",
"Temecula",
"Burbank",
"Daly City",
"Rialto",
"Santa Maria",
"El Cajon",
"San Mateo",
"Clovis",
"Compton",
"Jurupa Valley",
"Vista",
"South Gate",
"Mission Viejo",
"Vacaville",
"Carson",
"Hesperia",
"Santa Monica",
"Westminster",
"Redding",
"Santa Barbara",
"Chico",
"Newport Beach",
"San Leandro",
"San Marcos",
"Whittier",
"Hawthorne",
"Citrus Heights",
"Tracy",
"Alhambra",
"Livermore",
"Buena Park",
"Menifee",
"Hemet",
"Lakewood",
"Merced",
"Chino",
"Indio",
"Redwood City",
"Lake Forest",
"Napa",
"Tustin",
"Bellflower",
"Mountain View",
"Chino Hills",
"Baldwin Park",
"Alameda",
"Upland",
"San Ramon",
"Folsom",
"Pleasanton",
"Union City",
"Perris",
"Manteca",
"Lynwood",
"Apple Valley",
"Redlands",
"Turlock",
"Milpitas",
"Redondo Beach",
"Rancho Cordova",
"Yorba Linda",
"Palo Alto",
"Davis",
"Camarillo",
"Walnut Creek",
"Pittsburg",
"South San Francisco",
"Yuba City",
"San Clemente",
"Laguna Niguel",
"Pico Rivera",
"Montebello",
"Lodi",
"Madera",
"Santa Cruz",
"La Habra",
"Encinitas",
"Monterey Park",
"Tulare",
"Cupertino",
"Gardena",
"National City",
"Rocklin",
"Petaluma",
"Huntington Park",
"San Rafael",
"La Mesa",
"Arcadia",
"Fountain Valley",
"Diamond Bar",
"Woodland",
"Santee",
"Lake Elsinore",
"Porterville",
"Paramount",
"Eastvale",
"Rosemead",
"Hanford",
"Highland",
"Brentwood",
"Novato",
"Colton",
"Cathedral City",
"Delano",
"Yucaipa",
"Watsonville",
"Placentia",
"Glendora",
"Gilroy",
"Palm Desert",
"Cerritos",
"West Sacramento",
"Aliso Viejo",
"Poway",
"La Mirada",
"Rancho Santa Margarita",
"Cypress",
"Dublin",
"Covina",
"Azusa",
"Palm Springs",
"San Luis Obispo",
"Ceres",
"San Jacinto",
"Lincoln",
"Newark",
"Lompoc",
"El Centro",
"Danville",
"Bell Gardens",
"Coachella",
"Rancho Palos Verdes",
"San Bruno",
"Rohnert Park",
"Brea",
"La Puente",
"Campbell",
"San Gabriel",
"Beaumont",
"Morgan Hill",
"Culver City",
"Calexico",
"Stanton",
"La Quinta",
"Pacifica",
"Montclair",
"Oakley",
"Monrovia",
"Los Banos",
"Martinez"
],
"Illinois": [
"Chicago",
"Aurora",
"Rockford",
"Joliet",
"Naperville",
"Springfield",
"Peoria",
"Elgin",
"Waukegan",
"Cicero",
"Champaign",
"Bloomington",
"Arlington Heights",
"Evanston",
"Decatur",
"Schaumburg",
"Bolingbrook",
"Palatine",
"Skokie",
"Des Plaines",
"Orland Park",
"Tinley Park",
"Oak Lawn",
"Berwyn",
"Mount Prospect",
"Normal",
"Wheaton",
"Hoffman Estates",
"Oak Park",
"Downers Grove",
"Elmhurst",
"Glenview",
"DeKalb",
"Lombard",
"Belleville",
"Moline",
"Buffalo Grove",
"Bartlett",
"Urbana",
"Quincy",
"Crystal Lake",
"Plainfield",
"Streamwood",
"Carol Stream",
"Romeoville",
"Rock Island",
"Hanover Park",
"Carpentersville",
"Wheeling",
"Park Ridge",
"Addison",
"Calumet City"
],
"Texas": [
"Houston",
"San Antonio",
"Dallas",
"Austin",
"Fort Worth",
"El Paso",
"Arlington",
"Corpus Christi",
"Plano",
"Laredo",
"Lubbock",
"Garland",
"Irving",
"Amarillo",
"Grand Prairie",
"Brownsville",
"Pasadena",
"McKinney",
"Mesquite",
"McAllen",
"Killeen",
"Frisco",
"Waco",
"Carrollton",
"Denton",
"Midland",
"Abilene",
"Beaumont",
"Round Rock",
"Odessa",
"Wichita Falls",
"Richardson",
"Lewisville",
"Tyler",
"College Station",
"Pearland",
"San Angelo",
"Allen",
"League City",
"Sugar Land",
"Longview",
"Edinburg",
"Mission",
"Bryan",
"Baytown",
"Pharr",
"Temple",
"Missouri City",
"Flower Mound",
"Harlingen",
"North Richland Hills",
"Victoria",
"Conroe",
"New Braunfels",
"Mansfield",
"Cedar Park",
"Rowlett",
"Port Arthur",
"Euless",
"Georgetown",
"Pflugerville",
"DeSoto",
"San Marcos",
"Grapevine",
"Bedford",
"Galveston",
"Cedar Hill",
"Texas City",
"Wylie",
"Haltom City",
"Keller",
"Coppell",
"Rockwall",
"Huntsville",
"Duncanville",
"Sherman",
"The Colony",
"Burleson",
"Hurst",
"Lancaster",
"Texarkana",
"Friendswood",
"Weslaco"
],
"Pennsylvania": [
"Philadelphia",
"Pittsburgh",
"Allentown",
"Erie",
"Reading",
"Scranton",
"Bethlehem",
"Lancaster",
"Harrisburg",
"Altoona",
"York",
"State College",
"Wilkes-Barre"
],
"Arizona": [
"Phoenix",
"Tucson",
"Mesa",
"Chandler",
"Glendale",
"Scottsdale",
"Gilbert",
"Tempe",
"Peoria",
"Surprise",
"Yuma",
"Avondale",
"Goodyear",
"Flagstaff",
"Buckeye",
"Lake Havasu City",
"Casa Grande",
"Sierra Vista",
"Maricopa",
"Oro Valley",
"Prescott",
"Bullhead City",
"Prescott Valley",
"Marana",
"Apache Junction"
],
"Florida": [
"Jacksonville",
"Miami",
"Tampa",
"Orlando",
"St. Petersburg",
"Hialeah",
"Tallahassee",
"Fort Lauderdale",
"Port St. Lucie",
"Cape Coral",
"Pembroke Pines",
"Hollywood",
"Miramar",
"Gainesville",
"Coral Springs",
"Miami Gardens",
"Clearwater",
"Palm Bay",
"Pompano Beach",
"West Palm Beach",
"Lakeland",
"Davie",
"Miami Beach",
"Sunrise",
"Plantation",
"Boca Raton",
"Deltona",
"Largo",
"Deerfield Beach",
"Palm Coast",
"Melbourne",
"Boynton Beach",
"Lauderhill",
"Weston",
"Fort Myers",
"Kissimmee",
"Homestead",
"Tamarac",
"Delray Beach",
"Daytona Beach",
"North Miami",
"Wellington",
"North Port",
"Jupiter",
"Ocala",
"Port Orange",
"Margate",
"Coconut Creek",
"Sanford",
"Sarasota",
"Pensacola",
"Bradenton",
"Palm Beach Gardens",
"Pinellas Park",
"Coral Gables",
"Doral",
"Bonita Springs",
"Apopka",
"Titusville",
"North Miami Beach",
"Oakland Park",
"Fort Pierce",
"North Lauderdale",
"Cutler Bay",
"Altamonte Springs",
"St. Cloud",
"Greenacres",
"Ormond Beach",
"Ocoee",
"Hallandale Beach",
"Winter Garden",
"Aventura"
],
"Indiana": [
"Indianapolis",
"Fort Wayne",
"Evansville",
"South Bend",
"Carmel",
"Bloomington",
"Fishers",
"Hammond",
"Gary",
"Muncie",
"Lafayette",
"Terre Haute",
"Kokomo",
"Anderson",
"Noblesville",
"Greenwood",
"Elkhart",
"Mishawaka",
"Lawrence",
"Jeffersonville",
"Columbus",
"Portage"
],
"Ohio": [
"Columbus",
"Cleveland",
"Cincinnati",
"Toledo",
"Akron",
"Dayton",
"Parma",
"Canton",
"Youngstown",
"Lorain",
"Hamilton",
"Springfield",
"Kettering",
"Elyria",
"Lakewood",
"Cuyahoga Falls",
"Middletown",
"Euclid",
"Newark",
"Mansfield",
"Mentor",
"Beavercreek",
"Cleveland Heights",
"Strongsville",
"Dublin",
"Fairfield",
"Findlay",
"Warren",
"Lancaster",
"Lima",
"Huber Heights",
"Westerville",
"Marion",
"Grove City"
]
}
Just need to remove the brackets and store in an array so I could iterate through and map.
You can parse the JSON and then store it in State
const parsedValue = JSON.parse(JSON);
setState(parsedValue)
If it's not dynamic data, I would suggest storing huge array in separate file, import it in your component and straightaway store in state.

How do I use for loop with zip to combine two lists into separate sentences

I have:
cities = ["Phoenix", "Austin", "San Diego", "New York"]
states = ["Arizona", "Texas", "California", "New York"]
and I want to use for loop and zip to get:
Phoenix is in Arizona
Austin is in Texas
San Diego is in California
New York is in New York
This should solve your issue.
const cities = ["Phoenix", "Austin", "San Diego", "New York"];
const states = ["Arizona", "Texas", "California", "New York"];
for (let i = 0; i < cities.length; i++) {
console.log(cities[i] + "is in " + states[i])
}
You could also combine the two arrays into one.
const cities = ["Phoenix", "Austin", "San Diego", "New York"];
const states = ["Arizona", "Texas", "California", "New York"];
const zip = cities.map((city, index) => [city, states[index]]);
for (let i = 0; i < cities.length; i++) {
console.log(zip[i][0] + "is in " + zip[i][1])
}

Discord.js - JSON Parsing?

Okay, I'm a bit new to JSON in Discord.js Bot Development, And I'm currently making a command where it grabs all of the data in a json file and splits it so it looks nicely formatted.
Currently have:
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
function getGames(lol) {
lol = JSON.parse(Games[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]);
for (let j in lol) {
return "• " + lol[j];
}
}
I want to group all of the Games and make it output:
"• example\n"
Any answers?
You can get all object values, use map to add • and join it with '\n'.
Like this:
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
console.log(getGames(Games))
function getGames(gameList) {
return Object.values(gameList).map(game => `• ${game}`).join('\n')
}
Live example
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
console.log(getGames(Games))
function getGames(gameList) {
return Object.values(gameList).map(game => `• ${game}`).join('\n')
}
You can get an array of the object values with Object.values()
let gamenames = Object.values(games)
Then,you can loop through the array elements to add them to a string.
let myString = "List: \n"
gamenames.forEach((val) => {
myString += `• ${val} \n`
})
myString is now equal to an organized list of the games! It should look like:
List:
• Jailbreak
• Adopt me
...etc..
(The list does put every single value on a seperate line,but StackOverflow doesn't want to show them properly for some reason)
Hope this helped! If it did,please mark my question as a valid answer.

Codename One - Flags for all countries

I have a code very similar to the Uber clone app to show a country picker form. I understand that the flags are taken from flagResource = Resources.open("/flags.res");, from the Codename One SMS Library
The problem is that a lot of flags are missing. A user may be sorry if the flag of his/her country is not there.
How can I insert all the flags?
I wish I knew. I looked a lot for a resource that contains all of the flags in the world and was very disappointed that most were lacking in various ways.
After I did all that I thought about using the unicode emojii flags which would be a huge boon as it would also remove the need for images but that wouldn't work in the simulator properly as it doesn't render emojii. It might be interesting to try and enhance the code with support for emojii on devices where it's supported but you will need to actually do the legwork of going through almost 200 countries and finding their flag unicode value... There are some lists but adapting them is a pain.
I found a solution to show all the flags. I'm going to share my code and exactly how I proceeded...
STEP 1 - HOW MANY COUNTRY CODES?
Complete List of Country & Dialing Codes (248 country codes)
- https://www.worldatlas.com/aatlas/ctycodes.htm
I compared the previous list with these:
https://en.wikipedia.org/wiki/List_of_country_calling_codes (280
country code, but missing of iso codes)
https://countrycode.org/ (240 country codes)
http://www.countryareacode.net/ (225 country codes)
https://www.studyabroad.com/resources/international-calling-codes
(234 country codes)
https://www.howtocallabroad.com/codes.html (255 country codes,
but some of them are repeated)
The first list with 248 country codes is probabily the one to be used. Note that 248 country codes were used also by Whatsapp (in the 2016), it's an hint of the right direction:
https://www.quora.com/Whatsapp-has-248-flags-in-its-symbols-keyboard-in-its-latest-update-But-there-are-nearly-190-200-countries-on-earth-What-is-it-that-I-am-missing
However the current version of Whatsapp has 258 flags (I counted them extracting them from its apk). However, even if I suppose that the flags cannot fall within anyone's intellectual property, I think that it's safer to use flags from an open source repository, like the following one.
I'm going to use the 248 country codes list because it's the more suitable for our purposes (in my opinion, after several checkings).
STEP 2 - DOWNLOAD THE FLAGS
The 248 flags (in SVG format) can be downloaded from:
https://github.com/joielechong/iso-country-flags-svg-collection
The interesting folder for our purposes is this one:
iso-country-flags-svg-collection-master/svg/country-4x3
It contains more flags than the 248 available country codes (they are 262 in total), but it's not a problem (the opposite should be a problem, of course...).
I converted all of them to PNGs files with the size of 150x112 pixels with the following command (on Linux with Inkscape installed):
for i in *.svg; do inkscape $i --export-width=150 --export-height=112 --export-png=`echo $i | sed -e 's/svg$/png/'`; done
You can download these generated PNGs from: http://jmp.sh/vXaA7eC
STEP 3 - CREATE NEW ARRAYS FOR THE APP
In the Uber clone, we have the following arrays, each of them contains 243 strings:
COUNTRY_NAMES (Afghanistan to Zimbabwe, note that Åland Islands is at
the end of the list after Zimbabwe, I suppose that it's not correct
in alphabetic order... however the country code of Åland Islands is
the same of Finland)
COUNTRY_CODES (all numeric codes, without the "+")
COUNTRY_FLAGS (all the files names of the PNGs, with "null" for some countries)
COUNTRY_ISO2 (AL, DZ, AS, etc.)
COUNTRY_ISO3 (AFG, ALB, etc.)
I replaced the import of these array from com.codename1.sms.activation.ActivationForm.* with the following ones declared in my package (all of them contains 248 strings):
public static final String[] COUNTRY_NAMES = {"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bonaire", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Democratic Republic of the Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic", "Cote d'Ivoire", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, the Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Palestine, State of", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Romania", "Russian Federation", "Rwanda", "Reunion", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin (French part)", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Sint Maarten (Dutch part)", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "United Republic of Tanzania", "Thailand", "Timor-Leste", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Viet Nam", "Virgin Islands (British)", "US Virgin Islands", "Wallis and Futuna", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"};
// note: from the original list https://www.worldatlas.com/aatlas/ctycodes.htm, I modified the code of "Dominican Republic", I removed the "-" sign from all codes and I corrected some typos in the country names
public static final String[] COUNTRY_CODES = {"93", "355", "213", "1684", "376", "244", "1264", "672", "1268", "54", "374", "297", "61", "43", "994", "1242", "973", "880", "1246", "375", "32", "501", "229", "1441", "975", "591", "599", "387", "267", "47", "55", "246", "673", "359", "226", "257", "855", "237", "1", "238", "1345", "236", "235", "56", "86", "61", "61", "57", "269", "242", "243", "682", "506", "385", "53", "599", "357", "420", "225", "45", "253", "1767", "18", "593", "20", "503", "240", "291", "372", "251", "500", "298", "679", "358", "33", "594", "689", "262", "241", "220", "995", "49", "233", "350", "30", "299", "1473", "590", "1671", "502", "44", "224", "245", "592", "509", "672", "379", "504", "852", "36", "354", "91", "62", "98", "964", "353", "44", "972", "39", "1876", "81", "44", "962", "7", "254", "686", "850", "82", "965", "996", "856", "371", "961", "266", "231", "218", "423", "370", "352", "853", "389", "261", "265", "60", "960", "223", "356", "692", "596", "222", "230", "262", "52", "691", "373", "377", "976", "382", "1664", "212", "258", "95", "264", "674", "977", "31", "687", "64", "505", "227", "234", "683", "672", "1670", "47", "968", "92", "680", "970", "507", "675", "595", "51", "63", "870", "48", "351", "1", "974", "40", "7", "250", "262", "590", "290", "1869", "1758", "590", "508", "1784", "685", "378", "239", "966", "221", "381", "248", "232", "65", "1721", "421", "386", "677", "252", "27", "500", "211", "34", "94", "249", "597", "47", "268", "46", "41", "963", "886", "992", "255", "66", "670", "228", "690", "676", "1868", "216", "90", "993", "1649", "688", "256", "380", "971", "44", "1", "1", "598", "998", "678", "58", "84", "1284", "1340", "681", "212", "967", "260", "263"};
// here I list the file names in the same order of the previous country names and country codes; some unused flags are not listed, because we have more flags than country codes...
public static final String[] COUNTRY_FLAGS = {"af.png", "al.png", "dz.png", "as.png", "ad.png", "ao.png", "ai.png", "aq.png", "ag.png", "ar.png", "am.png", "aw.png", "au.png", "at.png", "az.png", "bs.png", "bh.png", "bd.png", "bb.png", "by.png", "be.png", "bz.png", "bj.png", "bm.png", "bt.png", "bo.png", "bq.png", "ba.png", "bw.png", "bv.png", "br.png", "io.png", "bn.png", "bg.png", "bf.png", "bi.png", "kh.png", "cm.png", "ca.png", "cv.png", "ky.png", "cf.png", "td.png", "cl.png", "cn.png", "cx.png", "cc.png", "co.png", "km.png", "cg.png", "cd.png", "ck.png", "cr.png", "hr.png", "cu.png", "cw.png", "cy.png", "cz.png", "ci.png", "dk.png", "dj.png", "dm.png", "do.png", "ec.png", "eg.png", "sv.png", "gq.png", "er.png", "ee.png", "et.png", "fk.png", "fo.png", "fj.png", "fi.png", "fr.png", "gf.png", "pf.png", "tf.png", "ga.png", "gm.png", "ge.png", "de.png", "gh.png", "gi.png", "gr.png", "gl.png", "gd.png", "gp.png", "gu.png", "gt.png", "gg.png", "gn.png", "gw.png", "gy.png", "ht.png", "hm.png", "va.png", "hn.png", "hk.png", "hu.png", "is.png", "in.png", "id.png", "ir.png", "iq.png", "ie.png", "im.png", "il.png", "it.png", "jm.png", "jp.png", "je.png", "jo.png", "kz.png", "ke.png", "ki.png", "kp.png", "kr.png", "kw.png", "kg.png", "la.png", "lv.png", "lb.png", "ls.png", "lr.png", "ly.png", "li.png", "lt.png", "lu.png", "mo.png", "mk.png", "mg.png", "mw.png", "my.png", "mv.png", "ml.png", "mt.png", "mh.png", "mq.png", "mr.png", "mu.png", "yt.png", "mx.png", "fm.png", "md.png", "mc.png", "mn.png", "me.png", "ms.png", "ma.png", "mz.png", "mm.png", "na.png", "nr.png", "np.png", "nl.png", "nc.png", "nz.png", "ni.png", "ne.png", "ng.png", "nu.png", "nf.png", "mp.png", "no.png", "om.png", "pk.png", "pw.png", "ps.png", "pa.png", "pg.png", "py.png", "pe.png", "ph.png", "pn.png", "pl.png", "pt.png", "pr.png", "qa.png", "ro.png", "ru.png", "rw.png", "re.png", "bl.png", "sh.png", "kn.png", "lc.png", "mf.png", "pm.png", "vc.png", "ws.png", "sm.png", "st.png", "sa.png", "sn.png", "rs.png", "sc.png", "sl.png", "sg.png", "sx.png", "sk.png", "si.png", "sb.png", "so.png", "za.png", "gs.png", "ss.png", "es.png", "lk.png", "sd.png", "sr.png", "sj.png", "sz.png", "se.png", "ch.png", "sy.png", "tw.png", "tj.png", "tz.png", "th.png", "tl.png", "tg.png", "tk.png", "to.png", "tt.png", "tn.png", "tr.png", "tm.png", "tc.png", "tv.png", "ug.png", "ua.png", "ae.png", "gb.png", "us.png", "um.png", "uy.png", "uz.png", "vu.png", "ve.png", "vn.png", "vg.png", "vi.png", "wf.png", "eh.png", "ye.png", "zm.png", "zw.png"};
// note that the file names should be lowercase, instead the country iso codes should be uppercase
public static final String[] COUNTRY_ISO2 = {"AF", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", "HR", "CU", "CW", "CY", "CZ", "CI", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RO", "RU", "RW", "RE", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW"};
public static final String[] COUNTRY_ISO3 = {"AFG", "ALB", "DZA", "ASM", "AND", "AGO", "AIA", "ATA", "ATG", "ARG", "ARM", "ABW", "AUS", "AUT", "AZE", "BHS", "BHR", "BGD", "BRB", "BLR", "BEL", "BLZ", "BEN", "BMU", "BTN", "BOL", "BES", "BIH", "BWA", "BVT", "BRA", "IOT", "BRN", "BGR", "BFA", "BDI", "KHM", "CMR", "CAN", "CPV", "CYM", "CAF", "TCD", "CHL", "CHN", "CXR", "CCK", "COL", "COM", "COG", "COD", "COK", "CRI", "HRV", "CUB", "CUW", "CYP", "CZE", "CIV", "DNK", "DJI", "DMA", "DOM", "ECU", "EGY", "SLV", "GNQ", "ERI", "EST", "ETH", "FLK", "FRO", "FJI", "FIN", "FRA", "GUF", "PYF", "ATF", "GAB", "GMB", "GEO", "DEU", "GHA", "GIB", "GRC", "GRL", "GRD", "GLP", "GUM", "GTM", "GGY", "GIN", "GNB", "GUY", "HTI", "HMD", "VAT", "HND", "HKG", "HUN", "ISL", "IND", "IDN", "IRN", "IRQ", "IRL", "IMN", "ISR", "ITA", "JAM", "JPN", "JEY", "JOR", "KAZ", "KEN", "KIR", "PRK", "KOR", "KWT", "KGZ", "LAO", "LVA", "LBN", "LSO", "LBR", "LBY", "LIE", "LTU", "LUX", "MAC", "MKD", "MDG", "MWI", "MYS", "MDV", "MLI", "MLT", "MHL", "MTQ", "MRT", "MUS", "MYT", "MEX", "FSM", "MDA", "MCO", "MNG", "MNE", "MSR", "MAR", "MOZ", "MMR", "NAM", "NRU", "NPL", "NLD", "NCL", "NZL", "NIC", "NER", "NGA", "NIU", "NFK", "MNP", "NOR", "OMN", "PAK", "PLW", "PSE", "PAN", "PNG", "PRY", "PER", "PHL", "PCN", "POL", "PRT", "PRI", "QAT", "ROU", "RUS", "RWA", "REU", "BLM", "SHN", "KNA", "LCA", "MAF", "SPM", "VCT", "WSM", "SMR", "STP", "SAU", "SEN", "SRB", "SYC", "SLE", "SGP", "SXM", "SVK", "SVN", "SLB", "SOM", "ZAF", "SGS", "SSD", "ESP", "LKA", "SDN", "SUR", "SJM", "SWZ", "SWE", "CHE", "SYR", "TWN", "TJK", "TZA", "THA", "TLS", "TGO", "TKL", "TON", "TTO", "TUN", "TUR", "TKM", "TCA", "TUV", "UGA", "UKR", "ARE", "GBR", "USA", "UMI", "URY", "UZB", "VUT", "VEN", "VNM", "VGB", "VIR", "WLF", "ESH", "YEM", "ZMB", "ZWE"};
STEP 4 - Put all together
From CountryCodePicker.java and CountryPickerForm.java (of the Uber Clone) remove the import of the arrays from com.codename1.sms.activation.ActivationForm and insert the previous arrays in your package and import them from it.
In the Codename One Designer, create a new file 248flags.res and import all the PNG flags generated at the Step 2 (http://jmp.sh/vXaA7eC) with "Images" -> "Quick Add Multiimages", selecting "4K" as source resolution. Wait for a while, it can be a long task...
In the CountryCodePicker.java, change:
flagResource = Resources.open("/flags.res");
to:
flagResource = Resources.open("/248flags.res");
STEP 5 - Result
Good news: now there are all the flags!!! :)
About the build size, the original flag.res of SMSActivation.cn1lib has a size of 411 kbyte (it's located in lib/impl/cls/flags.res), while my 248flags.res has a size of 2596 kbyte... but the original flag.res uses normal images of about 100x50 pixels, instead I used multiimages that better adapt on different densities.
Final check: it's possibile to manually check all country names and codes on Wikipedia and on other resources... if somebody wants to do the check, thanks :)

Excel macros Help. Creating a new column based of arrays.

I am trying to write a Macros in excel to create a new column with these states divided into these regions. I keep getting runtime error 13
Here is the code I have so far.
Sub Region ()
Dim Pacific As Variant
Pacific = Array("WA", "OR", "ID", "CA", "NV", "AZ", "NM", "HI", "AK")
Dim Continental As Variant
Continental = Array("AR", "IA", "CO", "KS", "LA", "MS", "MT", "ND", "NE", "OK", "SD", "UT", "WY")
Dim SouthEast As Variant
SouthEast = Array("GA", "AL", "FL", "SC", "KY", "TN")
Dim Midwest As Variant
Midwest = Array("MN", "WI", "IL", "IN", "MI", "OH")
Dim NorthAtlantic As Variant
NorthAtlantic = Array("ME", "NH", "MA", "RI", "CT", "VT", "NY", "PA", "NJ", "DE", "MD", "WV", "VA", "NC")
Dim Texas As Variant
Texas = Array("TX”)
Dim state As String , result As String
score = Range("F1").Value
If state = Pacific Then
result = "PACIFIC"
ElseIf state = Continental Then
result = "Continental"
ElseIf state = SouthEast Then
result = "SouthEast"
ElseIf state = Midwest Then
result = "Midwest"
ElseIf state = NorthAtlantic Then
result = "North Atlantic"
ElseIf state = Texas Then
result = "Texas"
Else
result = "fail"
End If
Range("Z1").Value = result
End Sub
AFAIK, to search for the occurrence of a string within an array isn't a simple matter within VBA. You either have to use a loop, or possibly use WorksheetFunction.Match.
A simpler way may be to avoid arrays altogether - your code could be easily refactored to use a Select Case statement:
Sub Region ()
Dim state As String , result As String
state = Range("F1").Value
Select Case state
Case "WA", "OR", "ID", "CA", "NV", "AZ", "NM", "HI", "AK"
result = "PACIFIC"
Case "AR", "IA", "CO", "KS", "LA", "MS", "MT", "ND", "NE", "OK", "SD", "UT", "WY"
result = "Continental"
Case "GA", "AL", "FL", "SC", "KY", "TN"
result = "SouthEast"
Case "MN", "WI", "IL", "IN", "MI", "OH"
result = "Midwest"
Case "ME", "NH", "MA", "RI", "CT", "VT", "NY", "PA", "NJ", "DE", "MD", "WV", "VA", "NC"
result = "North Atlantic"
Case "TX"
result = "Texas"
Case Else
result = "fail"
End Select
Range("Z1").Value = result
End Sub
Note: You also had two code problems.
You had
score = Range("F1").Value
when I think you meant
state = Range("F1").Value
You had "TX” instead of "TX" - I'm not sure whether the ” causes a problem in your version of Excel, but it does in mine.
To extend this function so that it applies to all cells in column F, you will need to loop through each row:
Sub Region ()
Dim state As String , result As String
Dim lastRow As Long
Dim r As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For r = 1 to lastRow
state = .Cells(r, "F").Value
Select Case state
Case "WA", "OR", "ID", "CA", "NV", "AZ", "NM", "HI", "AK"
result = "PACIFIC"
Case "AR", "IA", "CO", "KS", "LA", "MS", "MT", "ND", "NE", "OK", "SD", "UT", "WY"
result = "Continental"
Case "GA", "AL", "FL", "SC", "KY", "TN"
result = "SouthEast"
Case "MN", "WI", "IL", "IN", "MI", "OH"
result = "Midwest"
Case "ME", "NH", "MA", "RI", "CT", "VT", "NY", "PA", "NJ", "DE", "MD", "WV", "VA", "NC"
result = "North Atlantic"
Case "TX"
result = "Texas"
Case Else
result = "fail"
End Select
.Cells(r, "Z").Value = result
Next
End With
End Sub
Em why don,t you use Access create tables as you did and then link to further logical tables you are going to create (I presume there is some practical use of the code you wrote) That is why access was created in the first place...

Resources