在string.gmatch中使用Lua string.gsub?(Lua string.gsub inside string.gmatch?)

编程入门 行业动态 更新时间:2024-10-23 04:45:57
在string.gmatch中使用Lua string.gsub?(Lua string.gsub inside string.gmatch?)

我创建了这个简单的示例脚本来输出食物列表。 如果食物是水果,那么水果的颜色也会显示出来。 我遇到的问题是处理“草莓”的不规则复数。

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

我无法弄清楚为什么这不起作用,就像我想要的那样!

I've created this simple example script to output a list of foods. If the food is a fruit then the color of the fruit will be displayed as well. The issue I'm having is in dealing with the irregular pluralization of 'strawberries.'

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

Right now the program outputs:

potatoes apples = green strawberries carrots crab-apples

What I want is:

potatoes apples = green strawberries = red carrots crab-apples

I can't figure out why this doesn't work like I want it to!

最满意答案

好吧,有一件事,你在这里拼错了草莓:

fruits = {apple = "green", orange = "orange", stawberry = "red"}

您也可以使用lua表作为集合,这意味着不需要嵌套循环搜索重复项。 它可以简化为:

fruits = {apple = "green", orange = "orange", strawberry = "red"} foods = {"potatoes", "apples", "strawberries", "carrots", "crab-apples"} function singular(word) return word:gsub("(%a+)ies$", "%1y"):gsub("(%a+)s$", "%1") end for _, food in ipairs(foods) do local single_fruit = singular(food) if fruits[single_fruit] then print(food .. " = " .. fruits[single_fruit]) else print(food) end end

Well for one thing, you miss-spelled strawberry here:

fruits = {apple = "green", orange = "orange", stawberry = "red"}

You can also work with lua tables as sets, which means that nested loop searching for duplicates is unnecessary. It can be simplified to something like:

fruits = {apple = "green", orange = "orange", strawberry = "red"} foods = {"potatoes", "apples", "strawberries", "carrots", "crab-apples"} function singular(word) return word:gsub("(%a+)ies$", "%1y"):gsub("(%a+)s$", "%1") end for _, food in ipairs(foods) do local single_fruit = singular(food) if fruits[single_fruit] then print(food .. " = " .. fruits[single_fruit]) else print(food) end end

更多推荐

本文发布于:2023-08-01 11:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357955.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:gmatch   string   gsub   Lua

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!