Hey guys,
how do i search for a given string pattern, for example "red", that will output all table-values that have the "red" in it like f.e. "Redstone", "Red Carpet", "Red Glass Pane" etc., but it also has to come up with f.e. "Powered Rail" and "Wired Modem". Think of a search function like in the Minecraft Inventory search, where you type in letters and every item with the string appears, giving less items the more accurate the search string gets.
How do i compare my table values for a given string pattern when my table would looks as follows, just for imagination, not be seen for a real code example
item[1].name = "Redstone"
item[2[.name = "Stone"
item[3].name = "Powered Rail"
etc.
etc.
Thx in advance...
search inside a table
Started by ZagKalidor, Oct 04 2014 07:05 PM
2 replies to this topic
#1
Posted 04 October 2014 - 07:05 PM
#2
Posted 04 October 2014 - 07:47 PM
A simple string.match is probably all you need to use for this case, and would allow you to open up the power of Lua patterns to those using your program.
local search = "red"
local matches = {}
for i, v in ipairs(item) do
if string.match(v.name, search) then
table.insert(matches, v.name)
end
end
#3
Posted 04 October 2014 - 09:35 PM
Very nice, thanks a lot.
Nice to see, that i was on the right road to this, it just seems, the road was on another planet
Nice to see, that i was on the right road to this, it just seems, the road was on another planet
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











