Jump to content

Get sub table name and table count


Recommended Posts

Hey there community, Im here again with a little problem that is giving me headache.

Well, Im going to be clear, is there some way to get the sub table name and get the table count using this method:

local tableTest = {
	["Room1"] = {},
	["Room2"] = {},
	["Room3"] = {},
	["Room4"] = {}
}

outputChatBox("Table Count: "..#tableTest or 0)
for i, roomName in ipairs(tableTest) do
	outputChatBox(roomName)
end

It always drops me that the Table Count is 0 and it doesn't output the Sub Table Names

I want to get the table count and get the sub table names: Room1, Room2, Room3, Room4

I hope you can help me. 

Regards.

Edited by Overkillz
Link to comment
  • Moderators

ipairs loop only works an array structure tables. 

indexes: 1,2,3,4

The  #  only works for array structured tables.

 

 

But yours is an object/custom structured table.

indexes: 'Room1', 'Room2', 'Room3', 'Room4'

And for that you need the pairs loop

 

 

function getTableCount(thisTable)
	local count = 0
	for k, v in pairs(thisTable) do
		count = count + 1
	end
	return count
end


local count = getTableCount(tableTest)

 

@Overkillz

 

 

 

 

Edited by IIYAMA
  • Like 1
Link to comment
11 minutes ago, IIYAMA said:

ipairs loop only works an array structure tables. 

indexes: 1,2,3,4

The  #  only works for array structured tables.

 

 

But yours is an object/custom structured table.

indexes: 'Room1', 'Room2', 'Room3', 'Room4'

And for that you need the pairs loop

 

 


function getTableCount(thisTable)
	local count = 0
	for k, v in pairs(thisTable) do
		count = count + 1
	end
	return count
end


local count = getTableCount(tableTest)

 

@Overkillz

Alright, got it, but what about to get the name of those objects wihtout adding any thing inside each table.

EDIT: GOT IT. Looping it using pairs gives me the name.

Thanks matte :)

Edited by Overkillz
Link to comment
  • Moderators
9 minutes ago, Overkillz said:

Alright, got it, but what about to get the name of those objects wihtout adding any thing inside each table.

then you should start with an array structured table and create and object structured table afterwards.

 

 

Start with an array structured table.

local tableTestArray = {
	{key = "Room1"},
	{key = "Room2"},
	{key = "Room3"},
	{key = "Room4"}
}

local tableTestObject = {}

for i=1, #tableTestArray do
	tableTestObject[tableTestArray[i].key] = tableTestArray[i]
end

 

This allows you to access the data with multiple ways.

  • access to ipairs (loop in order)  --> tableTestArray
  • access to # (item count) --> tableTestArray
  • access with "Room1" --> tableTestObject

 

NOTE: the sub tables are LINKED between: tableTestArray <> tableTestObject

They are the same.

Edited by IIYAMA
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...