A question about tables

Hello, today I tried to experiment with tables. It worked pretty well until I tried to use a table used in HD admin commands. Now I am wondering how to extract variables out of the “Ranks” table.

The table:

Ranks = {			
	{5, "Owner",		};
	{4, "HeadAdmin",	{"Name Here", "Another Name", 0},  };			
	{3, "Admin",		{"Name Here", "Another Name", 0},  };
	{2, "Moderator",	{"Name Here", "Another Name", 0},  };
	{1, "VIP",			{"Name Here", "Another Name", 0},  };
	{0, "None",			};

}

I do not know any way to extract the “Another Name” value (Line 3) for example. I hope someone can help me with this because this will probably help me in the future.

for i,v in pairs(Ranks)do
   local AnotherName = v[2][2]
end

Basically, within tables, you can use [x] to obtain something listed in the said position (x)
for example,

local colours = {'blue', 'red', 'green'}

print(colours[3]) -- prints 'green'
print(colours[2]) -- prints 'red'

I have tried it and this is in the output: Screenshot 2020-12-20 215755

The code Used:

    Ranks = {			
    	{5, "Owner",		};
    	{4, "HeadAdmin",	{"Name Here", "Another Name", 0},  };			
    	{3, "Admin",		{"Name Here", "Another Name", 0},  };
    	{2, "Moderator",	{"Name Here", "Another Name", 0},  };
    	{1, "VIP",			{"Name Here", "Another Name", 0},  };
    	{0, "None",			};

    }

    for i, v in pairs(Ranks) do
    	local AnotherName = v[2][2]
    	print(AnotherName)
    end

Apologies, I meant

v[3][2]

Of course, you will need to check if it exists or it will error.

for i, v in pairs(Ranks) do
    local v2 = v[3] -- If there is a table in that rank
    if v2 then
        local anotherName = v2[2]
    end
end

I am still wondering why it is printed 6 times:
image

Are you trying to find a singular index in the table?

It works like this.

Ranks[2] is
{4, "HeadAdmin", {"Name Here", "Another Name", 0}, };
Ranks[2][1] is 4,
Ranks[2][2] is “HeadAdmin”,
and Ranks[2][3] is
{"Name Here", "Another Name", 0}
So Ranks[2][3][1] is “Name Here” and Ranks[2][3][3] is 0*

1 Like