table size, empty tables and adding elements to a table

Post Reply
Metho
Posts: 14
Joined: Wed Sep 16, 2009 10:20 pm

table size, empty tables and adding elements to a table

Post by Metho »

Alright, me again. I've been looking for a reason why in LUA help, but can't find one, so maybe you guys can tell me. I have this code (Yes, this damn code again!):

Code: Select all

if myEnemies == nil then
    myEnemies = {};
end

local newenemy = matches[2]

for k,v in pairs(myEnemies) do
    if v == newenemy then
        echo("" .. newenemy .. " is already an enemy!\n")
    else
        echo("" .. newenemy .. " is now an enemy!\n")
        listAdd( myEnemies, newenemy )
        break
    end
end
Now, this code WORKS as long as myEnemies is not empty. If the table myEnemies is empty, nothing happens. I do en Test, and I get no feedback. No echo, no add to the table, and no error in debug. If I manually add Test to the table, and then try to en TestAgain, it works! I get TestAgain is now an enemy!, I can add other names, remove the original entry, blah, blah, UNTIL I remove all entries, and then we're back to broke.

So I thought that maybe I need to check for it being empty, as maybe for .. do breaks under an empty table, so I tried:

Code: Select all

if myEnemies == nil then
    myEnemies = {};
end

local newenemy = matches[2]

for k,v in pairs(myEnemies) do
    if IsEmpty(myEnemies) then
        echo("No enemies!")
        break
    elseif v == newenemy then
        echo("" .. newenemy .. " is already an enemy!\n")
    else
        echo("" .. newenemy .. " is now an enemy!\n")
        listAdd( myEnemies, newenemy )
        break
    end
end
The code compiles, but still when I try it nothing happens, no response, no error. Any clues?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Code does not work when table empty

Post by Heiko »

I've changed to topic title for a more precise topic reference for other users in case they have similar problems with tables.

You haven't provided the code of your IsEmpty() function. So I can't comment on your second script.

To get the size of a table you can use the Lua #-operator e.g.: numberOfEnemies = # myEnemies

Your first script doesn't work because of wrong program logic. You are iterating over all keys in the table and add a new enemy to the table for each key that doesn't match newenemy. Consequently, you don't add a new enemy to the table if the table is empty and if it is not empty you'll add the new enemy for each key that is not equal to newenemy. This is obviously not what you want.

Instead you'll want to check if neweney is already a member of your table and if it isn't you want to add it.

Code: Select all

local isMember = false;
for k,v in ipairs(myEnemies) do
    if v == newenemy then isMember = true; end;
end
if isMember == false then table.insert( myEnemies, newenemy ); end;

Metho
Posts: 14
Joined: Wed Sep 16, 2009 10:20 pm

Re: table size, empty tables and adding elements to a table

Post by Metho »

Damn. Thank you.

That first script with the IsEmpty()... I was typing in Ruby instead of LUA for a second there. Forgot LUA doesn't have the function.

Post Reply