Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Indexing a multi-dimensional table?

Asked by
saenae 318 Moderation Voter
9 years ago

I've decided to try making my combat system using a multi-dimensional table, but I'm having some trouble: The indices of the tables (excluding the last one in the stack) are no longer numerical, and I'm therefore unable to work with them as I normally do. I went around this by ending each table's name with a number, then using tonumber(string.sub(tableName, endofTableName)). However, this doesn't help me when I'm attempting to find the length of the table, because #Table simply values at 0. Any ideas on how to fix this? Thanks a ton! :) --EDIT-- To clear up the question a bit, since it's pretty hard to understand, let me give an example;

Attack = {
    Combo = {
        Pose1 = {
            1, 2, 3
        },
        Pose2 = {
            1, 2, 3
        }
    }
}

How would you find the length of the table Combo? #Attack.Combo wouldn't work, as that would simply return 0. Sorry about the question being hard to understand, I often have a hard time explaining myself.

0
Editing ;) XToonLinkX123 580 — 9y

3 answers

Log in to vote
1
Answered by
funyun 958 Moderation Voter
9 years ago

I got something for you.

mda = {
    t1 = {
        t2 = {
            hi = "hi"
        }
    }
}

function length(t)
    assert(type(t) == "table", "Argument must be a table.")
    local elements = 0

    for _, v in pairs(t) do
        elements = elements + 1
    end

    return elements
end

print(length(mda.t1.t2))
2
That's almost exactly what I ended up doing after a little more thought haha. Thanks so much though, glad to see we think alike! :P saenae 318 — 9y
2
Sorry, didn't see your answer. Spongocardo 1991 — 9y
2
...That's 4 people thinking of the same answer unknowingly. funyun 958 — 9y
1
That's very strange, ha ha! Spongocardo 1991 — 9y
Ad
Log in to vote
3
Answered by 9 years ago

Oh ok, Thanks for more detailled it. What I'll do for now it show you how to got the lenght of a table. First we'll create a function named "getTableLenght" with parametre "t"(table) in this function we'll declare a variable named "lenght" or whatever you want with the value 0. After we'll get all thing in the table you want to got the childs and adding 1 when he found child.Finally we returnning the lenght of the table.

Attack = {-- the multi-table here
    Combo = {
        Pose1 = {
            1, 2, 3
        },
        Pose2 = {
            1, 2, 3
        }
    }
}


function getTableLenght(t)--t is table
    assert(type(t) == "table", "Table expected !") -- check if the parametre 't' is a table else ending the function
    local lenght = 0; -- declared the lenght of the table
    for i, v in pairs(t) do -- got all thing in the table
        lenght = lenght + 1; -- add 1 to the variable "lenght" when found a child
    end
    return lenght; -- Returnning the lenght(should return 2)
end

local t = getTableLenght(Attack.Combo); --running the function
print(t);--printting lenght of the table

for more nice you can also do:

Attack = {-- the multi-table here
    Combo = {
        Pose1 = {
            1, 2, 3
        },
        Pose2 = {
            1, 2, 3
        }
    }
}


function getTableLenght(t)--t is table
assert(type(t) == "table", "Table expected !") -- check if the parametre 't' is a table else ending the function
    local lenght, result = 0, false; -- declared the lenght of the table and the result with a defaul value as "false"
    for i, v in pairs(t) do -- got all thing in the table
        if type(v) == "table" then --look if a table is in the table
            result = true; --set the value of result
        end
        lenght = lenght + 1; -- add 1 to the variable "lenght" when found a child
    end
    return lenght, true; -- Returnning the lenght(should return 2) and true
end

local t, otherTable = getTableLenght(Attack.Combo); --running the function
print(t, otherTable);--printting lenght of the table and true
Log in to vote
2
Answered by 9 years ago

Instead of using # (which only works for numerical indexes), you could just add one to a variable for each index that is found when looping through a table using a for loop.

For example, you could createa function like so which returns the number of indexes in a table.

function GetTabLength(table)
    if type(table) ~= "table" then return end --Stop the function if table is nil.
    local len = 0 --Start with a length variable of 0.
    for i,_ in pairs(table) do --Loops through each index of the table.
        len = len + 1 --Adds one to the length variable for each table index.
    end
    return len --Return the final len value for use.
end

print(GetTabLength(Attack.Combo)) --Should print 2 as there is two indexes.

I hope my answer helped you. If it did, be sure to accept it.

2
Beat you to it funyun 958 — 9y
1
Ha ha, I always get pipped to the post because I try and find things to make my code elegant then realize I'm not the only one answering . Spongocardo 1991 — 9y
1
Same for you )': XToonLinkX123 580 — 9y

Answer this question