Try this script. Uses associative arrays as before, but this time does a contains search for a string within a string. So contains("applesauce", "apple") will find a match.
// Create an associative array for the different types of food
food_aa = associative array();
food_aa["Fruit"] = {"apple", "pear", "banana", "plum"};
food_aa["Meat"] = {"ham", "chicken", "beef", "fish"};
food_aa["Flower"] = {"daisy", "sunflower", "aster", "peony"};
// List of foods to match up
food_items = List( "applesauce", "banana split", "hambone", "chicken", "daisyflower", "corvette" );
kind_of_food = {};
keylist = food_aa << get keys();
For( i = 1, i <= N Items( food_items), i++,
one_food = food_items[i];
for (k = 1, k <= nitems(keylist), k++,
one_key = keylist[k];
food_type_list = food_aa[one_key];
found_match = 0;
for (m = 1, m <= nitems(food_type_list), m++,
one_type_food = food_type_list[m];
if (contains(one_food, one_type_food),
insert into (kind_of_food, one_key);
print("Found match: " || one_key);
found_match = 1;
break(); // found a match - no need to keep looking at other foods
);
);
if (found_match, break()); // found a match - no need to keep looking for a food type
);
if (!found_match,
print("No match found for " || one_food)
);
);
Show( kind_of_food );
Here are the results from the log:
"Found match: Fruit"
"Found match: Fruit"
"Found match: Meat"
"Found match: Meat"
"Found match: Flower"
"No match found for corvette"
kind_of_food = {"Fruit", "Fruit", "Meat", "Meat", "Flower"};