MX_Master Posted October 23, 2010 Share Posted October 23, 2010 I have one table which contains some values and subtables. Any of subtable also may contains values and subtables, and etc. Look: myTable = { ["x"] = 0, ["y"] = 0, ["children"] = { [0] = { ["x"] = 10, ["y"] = 15, ["children"] = { [0] = { ["x"] = 20, ["y"] = 25, ["children"] = { [0] = { ["x"] = 30, ["y"] = 35 }, [1] = { ["x"] = 40, ["y"] = 45 } } }, [1] = { ["x"] = 50, ["y"] = 55 } } }, [1] = { ["x"] = 60, ["y"] = 65 } -- etc. } } I want to find ALL subtables of myTable and place this list into another table (named children) as simple list without hierarchy. Look: children = { [0] = { ["x"] = 10, ["y"] = 15 }, [1] = { ["x"] = 20, ["y"] = 25 }, [2] = { ["x"] = 30, ["y"] = 35 }, [3] = { ["x"] = 40, ["y"] = 45 }, [4] = { ["x"] = 50, ["y"] = 55 }, [5] = { ["x"] = 60, ["y"] = 65 } -- etc. } I know this can be done using recursion. But i need method without recursion. Link to comment
dzek (varez) Posted October 23, 2010 Share Posted October 23, 2010 its easier with recursion, why you want it w/out? Link to comment
MX_Master Posted October 23, 2010 Author Share Posted October 23, 2010 this operation will be placed into "onClientRender" client event and operation must be fast as possible Link to comment
Antibird Posted October 23, 2010 Share Posted October 23, 2010 How large your initial table is? Did you try to count how long it takes to look through it? For example on my core2Duo 1.66 it's like 15 ms to list through an array of a million of elements. Also I don't think there's a better way to do what you want rather than using "ipairs" iteration. Recursion is your friend in this case =) Link to comment
dzek (varez) Posted October 23, 2010 Share Posted October 23, 2010 and who told you recursion is slow thing? Link to comment
MX_Master Posted October 23, 2010 Author Share Posted October 23, 2010 never try and test recursion in LUA, but in PHP this operation with recursion takes more time and memory than method without recursion another reason for this is just to see how it's can be done.. just want to understand logic without recursion How large your initial table is? Did you try to count how long it takes to look through it? For example on my core2Duo 1.66 it's like 15 ms to list through an array of a million of elements.... i can write this operation with recursion, but i haven't "without recursion" method code to test it and compare with first one Link to comment
Antibird Posted October 23, 2010 Share Posted October 23, 2010 As you said you have a table which contain an undefined number of subtables which contain an undefined number of subtables which contain an undefined number of subtables which contain.. -> Means you need an undefined number of iterations to go through every table you have in the mess. -> Means you need a recursive function - iterator -- or -- an undefined number of non-recursive iterators to solve it. The logic still stays the same I guess: it's to catch every single (sub)table you have and put it into another. Regardless of the recursion level you do the same thing to every subtable (obviously). Just in case you want to see how it can be done without rec., I must say I don't really know any efficient ways. Well, you may transform your table into a string using toJSON() and then use string.gsub() to search for table-specific symbols , like "[ [ ... ] ]" I think, and then transform the table-containing-substring into lua table again and then put it into your "children" table.. Jut in case.. =) 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