Jump to content

dumptable causes error


Some1
  • Pending

There is a minor problem with dumtable function, if you use it to show tables with mixed type of keys.

Let's say, we have the following table: tbl = {a = 1, {} , ""} --a table with one string key named "a" and two number keys 1 and 2

dumptable contains table.sort function, which can't sort tables with mixed type of the keys, so the game would throw us an "attempt to compare string with number" error.

To avoid this you should add custom comparator, which would make the job, s.a.:

...
        --part of dumptable
        
        local table_keys = {}
        for k,v in pairs(obj) do
            if type(k) == "table" then
                table.insert(table_keys, k)
            else
                table.insert(keys, k)
            end
        end

        
        local function compareMixed(a, b)
      	    if type(a)~=type(b) then --convert both to strings first, then compare
                return tostring(a)<tostring(b)
            else
                return a<b --this should stay to maintain normal order for numbers (55 is greater than 8, while "55" is less than "8")
            end
        end
        
        --and only after that...
        table.sort(keys, compareMixed) --using comparator

...

 


Steps to Reproduce
tbl = {a = 1, {} , ""} dumtable(tbl)



User Feedback


There are no comments to display.



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

×
  • Create New...