【发布时间】:2015-03-13 10:42:23
【问题描述】:
我创建了这个简单的示例脚本来输出食物列表。如果食物是水果,那么也会显示水果的颜色。我遇到的问题是处理“草莓”的不规则复数形式。
fruits = {apple = "green", orange = "orange", stawberry = "red"}
foods = {"potatoes", "apples", "strawberries", "carrots", "crab-apples"}
for _, food in ipairs(foods) do
for fruit, fruit_colour in pairs(fruits) do
duplicate = false
if (string.match(food, "^"..fruit) or string.match((string.gsub(food, "ies", "y")), "^"..fruit)) and not(duplicate) then -- this is where the troubles is!
print(food.." = "..fruit_colour)
duplicate = true
break
end
end
if not(duplicate) then
print(food)
end
end
现在程序输出:
potatoes
apples = green
strawberries
carrots
crab-apples
我想要的是:
potatoes
apples = green
strawberries = red
carrots
crab-apples
我不明白为什么这不能像我想要的那样工作!
【问题讨论】:
标签: lua gsub lua-patterns