Question

I'm trying to match two tables by string case in sensitive.

Example

[6] = 
                {
                    ["itemName"] =     [Ore],
                    ["cleanName"] = [[Iron Ingot]],
                    ["secs"] = 25004,
                    ["gN"] = [[Luminary]],
                    ["buyer"] = [[@Naptha42]],
                    ["eventType"] = 15,
                    ["timestampz"] = 1399514069,
                    ["quantity"] = 100,
                    ["soldAmount"] = 500,
                    ["seller"] = [[@FataI]],
                },

I'm trying to move items from the above table format into a new table, or filter them into a list.

g = e.cleanName 


for k, v in pairs(savedTable.ALL_ITEMS) do searchTable[k] = v
if g == savedTable.ALL_ITEMS.cleanName then
   table.insert(searchTable, savedTable.ALL_ITEMS)
end
end

The problem above is g never = that savedTable.

The table has a few thousand entries and I'm trying to display by search, I believe the best way is to create a new table with the values im looking for?

Or is there a way to use the string.match to iterate through the table and display (print) only these values.

//Update I have an active search, each time the user enters a letter it will search the table. Example. Val1 = "Hello" Val2 = "Goodbye" Val3 = "Hi"

While searching and the user enters "H" or "h" I want the results for "hello, hi" to show up but not "goodbye"

so with the if g == string.match(savedTable.ALL_ITEMS.cleanName)

not sure if that is correct.

//UPDATE for solution of what worked (thanks ESOUI)

   local searchForItem = string.lower(g)
    for k, v in ipairs(myTable.ALL_SALES) do
        --        if string.lower(g) == string.lower(v.cleanName) then
        if string.lower(v.cleanName):find(searchForItem) then
            if v.cleanName ~= nil then
                table.insert(searchTable, v)
            end
        end
    end
Was it helpful?

Solution

If ALL_ITEMS is a table that contains tables then it isn't ever going to have a cleanName field directly.

Is that == in the loop intended to be against v?

Also, if ALL_ITEMS is integer indexed you probably want ipairs instead of pairs to walk the table in order.

Why are you inserting every element into searchTable (via searchTable[k] = v) and then also attempting to insert the matching entries again (via table.insert)?

You can use string.match to match g against the cleanName in your loop if you need to but that isn't going to solve the problem if you aren't comparing what you think you are comparing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top