3

I'm trying to find out if a certain array exists via an if statement such as

if array{} == nil then array = {} else print("it exists") end

The above doesn't work it seems and I have no way of checking if it exists, basically I'm creating an AddOn which scans a log for a certain event and if it's true it returns the spellName. I wish to create an array with that spellName, however spellName = {} doesn't work as it seems to just create a new array (rather than updating the existing one).

local _SPD = CreateFrame("Frame");
_SPD:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
_SPD:SetScript("OnEvent", function(self, event, ...)

local timestamp, type, sourceName = select(1, ...), select(2, ...), select(5, ...)

if (event == "COMBAT_LOG_EVENT_UNFILTERED") then
    if select(2,...) == "SPELL_AURA_APPLIED" then
        if select(5,...) == UnitName("player") then

            local spellID, spellName = select(12, ...), select(13, ...)

             spellName = { 
                sourceName = {

                } 
            }

            table.insert(spellName["sourceName"], {id = spellID, stamp = timestamp })

            for k,v in pairs ( spellName["sourceName"] ) do
                print (k.. ": " ..v["id"].. " at " ..v["stamp"])
            end 
        end
    end
end
end);

Basically it's just re-creating the table every time a certain aura is applied on me (which is expected behavior)

I've banged my head but I have no idea how to check if spellName (and sourceName) exists and if so do not create them again since in this case the variable already exists because it returns the value to me so I can't check if they're nil as they won't be, I need to somehow check if a table exists on those values and if not create them.

Thanks in advance.

2
  • 1
    local spellID, spellName = select(12, ...), select(13, ...) is silly. Get rid of the , select(13, ...) and just leave the select(12, ...). select() actually returns all values, starting with the given index, not just a single value, so saying local a, b = select(12, ...) will assign the 12th arg to a and the 13th arg to b. Commented Jan 30, 2013 at 22:46
  • @KevinBallard, that should save me some space, thanks. Commented Jan 30, 2013 at 23:07

2 Answers 2

2

Your declaration for table checking is wrong. Use it like this:

if type(array) == "table" then
  print("it exists")
else
  array = {}
end
Sign up to request clarification or add additional context in comments.

8 Comments

For what it's worth, this could be written as array = type(array) == "table" and array or {}.
You could have just written if type(array) == 'table' then -- which will return nil if array is nil, making the first test unnecessary.
@Mud hmm, I didn't know that type can take nil arguments. Edited.
Thanks for the help, I've added the following lines: print(spellName) if type(array) == spellName then print("it exists") else spellName = {} print(spellName) print("created") end and it seems to return the first print with the spellName but the one after the creation of the table returns with a table number (which is different everytime I run the spell while the first print stays the same.
You need to use if type(spellName) == "table" as your if condition.
|
1

Try this:

local spellID, spellName = select(12, ...), select(13, ...)
spellName = spellName or {}
spellName.sourceName = spellName.sourceName or {}
table.insert(spellName.sourceName, {id = spellID, stamp = timestamp })

3 Comments

Doesn't work unfortunately, returned an error Locals: (*temporary) = nil (*temporary) = <table> { id = 52127 stamp = 1359598562.074 } (*temporary) = "table expected, got nil"
@user2026598 - Edited my answer. Try again please.
didn't seem to work either, however adding myTable = {} at the top and using if myTable[spellName] then print("exists") else myTable[spellName] = {} end seemed to do the trick. Thanks to the following people in this thread: mmo-champion.com/threads/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.