Overkillz Posted March 7, 2019 Share Posted March 7, 2019 (edited) 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 March 7, 2019 by Overkillz Link to comment
Moderators IIYAMA Posted March 7, 2019 Moderators Share Posted March 7, 2019 (edited) 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 March 7, 2019 by IIYAMA 1 Link to comment
Overkillz Posted March 7, 2019 Author Share Posted March 7, 2019 (edited) 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 March 7, 2019 by Overkillz Link to comment
Moderators IIYAMA Posted March 7, 2019 Moderators Share Posted March 7, 2019 (edited) 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 March 7, 2019 by IIYAMA 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now