'LinKin Posted April 15, 2014 Share Posted April 15, 2014 Hello, I've a table, the key is a team's element, for example: team1 = getTeamFromName("ss") team2 = getTeamFromName("aa") team3 = getTeamFromName("dd") teamsTable[team1] = { name = "mmm", tag = "tag"} teamsTable[team2] = { name = "mmm", tag = "tag"} teamsTable[team3] = { name = "mmm", tag = "tag"} Then I use a for loop (in pairs) because (in ipairs) doesn't work. But working with in pairs loop generates an 'unorganized' functionallity. So for example I got this: for _, theTeam in pairs(teamsTable) do outputChatBox(tostring(_).." Team: "..theTeam.name) end It will display the teams without order, in this case it showed the teams in this order: team3 team1 team2 I want to make it in the same order as I "insterted" the teams in the table.. Can I do this by working with an element-key table? Or I am oblied to use an indexed table? Like table.instert(teamsTable, {name="ss", etc...} ) Thanks Link to comment
arezu Posted April 16, 2014 Share Posted April 16, 2014 Or I am oblied to use an indexed table? Like table.instert(teamsTable, {name="ss", etc...} ) Yes. Alternatively use a number in each table and use table.sort, but it's easier to insert to table with ordered index instead. Link to comment
Vinctus Posted April 16, 2014 Share Posted April 16, 2014 Why do you do it like that? why not just: local teamsTable = { {"team name", "team tag"}, {"team name", "team tag"}, } for i,theTeam in ipairs(teamsTable) do outputChatBox(tostring(i) .. " Team: " .. theTeam[1] .. " [" .. theTeam[2] .. "]") end 1 being the team name, 2 being the tag Link to comment
Moderators IIYAMA Posted April 16, 2014 Moderators Share Posted April 16, 2014 @Vinctus Because he can't access the data easily by using the team it self. @LinKin pairs doesn't have an order, even if it did then it wouldn't loop correctly since the keys are userdata's, which are random generated strings. team1 = getTeamFromName("ss") team2 = getTeamFromName("aa") team3 = getTeamFromName("dd") teamsTable[team1] = { name = "mmm", tag = "tag", order = 1} teamsTable[team2] = { name = "mmm", tag = "tag", order = 2} teamsTable[team3] = { name = "mmm", tag = "tag", order = 3} local orderTeams = {} for theTeam, theTeamData in pairs(teamsTable) do orderTeams[theTeamData.order]= {team = theTeam,data= theTeamData} end for i=1,#orderTeams do local whatEver = orderTeams[i] outputChatBox(tostring(whatEver.team) .. " Team: " .. whatEver.data.name ) end Link to comment
Vinctus Posted April 16, 2014 Share Posted April 16, 2014 @IIYAMA, in that case he could do this: local teamsTable = { ["ss"] = { name = "team name", tag = "team tag"}, ["aa"] = { name = "team name", tag = "team tag"}, ["dd"] = { name = "team name", tag = "team tag"}, } and access team name and tag by using teamsTable["the team name"].name teamsTable["the team name"].tag yes? Link to comment
Moderators IIYAMA Posted April 16, 2014 Moderators Share Posted April 16, 2014 Yes, but then you can't still use the ipairs loop, without order the table first like I did. Why would you use tags when userdata/element can access easier? The best solution would be using 2 tables. Link to comment
Vinctus Posted April 16, 2014 Share Posted April 16, 2014 Yes, but then you can't still use the ipairs loop, without order the table first like I did.Why would you use tags when userdata/element can access easier? The best solution would be using 2 tables. Yes you can Link to comment
Moderators IIYAMA Posted April 16, 2014 Moderators Share Posted April 16, 2014 Yes, but then you can't still use the ipairs loop, without order the table first like I did.Why would you use tags when userdata/element can access easier? The best solution would be using 2 tables. Yes you can To loop over an array, use ipairs. Unlike pairs, it only gives you the consecutive integer keys from 1, and it guarantees their order. With pairs, the number keys will not necessarily be given in the correct order! You can't. I have been working long with tables and I still do. I will recommend you to test it your self. >> http://www.lua.org/cgi-bin/demo local myTable = {["hi"]="lol",["hai"]="lol2"} for i,data in ipairs(myTable) do print(data .. " ipairs") end for i,data in pairs(myTable) do print(data .. " pairs") end AFAIK, there is a possibility using a metatable, but that is out of the question. 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