Problems using Lua tables

Let's say I have two tables:

veggie_multiples = {
{veggie = "carrot", quantity = 1},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 3}}

veggie_singles = {
{veggie = "celery"},
{veggie = "carrot"},
{veggie = "potato"},
{veggie = "carrot"},
{veggie = "potato"}}

      

I would like to get one table that represents:

veggie_multiples = {
{veggie = "carrot", quantity = 3},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 4},
{veggie = "potato", quantity = 2}}

      

I've tried something like:

veggie_multiples = {
{veggie = "carrot", quantity = 1},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 3}}

veggie_singles = {
{veggie = "celery"},
{veggie = "carrot"},
{veggie = "potato"},
{veggie = "carrot"},
{veggie = "potato"}}

for i, n in ipairs(veggie_singles) do
    for ii, nn in ipairs(veggie_multiples) do
        if veggie_singles[i].veggie == veggie_multiples[ii].veggie then
            veggie_multiples[ii].quantity = veggie_multiples[ii].quantity + 1
            table.remove(veggie_singles, i)
        else
            table.insert(veggie_multiples, {veggie = veggie_singles[i], quantity = 1})
            table.remove(veggie_singles, i)
        end
    end
end

for i, n in ipairs(veggie_multiples) do
    print(veggie_multiples[i].veggie, " ", veggie_multiples[i].quantity)
end

      

I cannot get it to work no matter what I try. Please help! Thank.

+3


source to share


2 answers


And for a loop using ipairs, the index is iterated and the value for i, n in ipairs(veggie_singles)

will be given i=1 and n={veggie="celery"}

in the first iteration and so on. Code shouldn't be used i

, so in Lua you use _ as an outlier. Next, find a Veggie entry that has the same name as veggie. Add it if not found or will not increase the quantity if found.

for _, vs in pairs(veggie_singles) do
    local found = false
    for _, vm in pairs(veggie_multiples) do
       if vm.veggie == vs.veggie then
          vm.quantity = vm.quantity + 1
          found = true
          break
       end
    end
    if not found then
        table.insert(veggie_multiples, {veggie=vs.veggie, quantity=1}) 
    end
end


for i, n in ipairs(veggie_multiples) do
    print(veggie_multiples[i].veggie, " ", veggie_multiples[i].quantity)
end

      

Since indexes are not very useful for the problem, and instead the name is what you want to find, you can use the names as keys in the tables to simplify your code.



quantity = {carrot=1, tomato=2, celery=3}
add = {"celery", "carrot", "potato", "carrot", "potato"}

for _, v in pairs(add) do
   quantity[v] = (quantity[v] or 0) + 1
end

for veggie, qty in pairs(quantity) do
   print(veggie, qty)
end

      

which outputs:

potato  2
carrot  3
celery  4
tomato  2

      

+3


source


It works:

for i, n in ipairs(veggie_singles) do
    local existed = false
    for ii, nn in ipairs(veggie_multiples) do
        if veggie_singles[i].veggie == veggie_multiples[ii].veggie then
            veggie_multiples[ii].quantity = veggie_multiples[ii].quantity + 1
            existed = true
            break
        end
    end
    if not existed then
        table.insert(veggie_multiples, {veggie = veggie_singles[i].veggie, quantity = 1})
    end
end

      



Only if the element is veggie_singles[i].veggie

not equal to all the elements in veggie_multiples

, a new element is inserted. This is what the flag does existed

.

+1


source







All Articles