[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lua-l
Subject:    Re: Making Lua state internal random seed optional?
From:       William Ahern <william () 25thandClement ! com>
Date:       2014-07-29 19:21:51
Message-ID: 20140729192151.GB21373 () wilbur ! 25thandClement ! com
[Download RAW message or body]

On Tue, Jul 29, 2014 at 05:06:12PM +0200, Benoit Germain wrote:
> Unfortunately I can't sort something like this (my data is more complex
> than that of course, I have 600 lines of simular Lua code per settings
> file):
> 
>                 ["pushForceCurve"] =
>                 {
>                     ["param1"] = -4,
>                     ["param3"] = 0.502515,
>                     ["param2"] = 2
>                     ["centerOfMassOffset"] =
>                     {
>                        ["x"] = 0,
>                        ["y"] = 0
>                     },
>                 },

I usually sort it in Lua. I have a sorted_pairs functio which I use when
writing out table data to file. For example,

for k,v in sorted_pairs(t) do
	-- write table data out
end


Here's the sorted_pairs function:

local function sorted_pairs(t)
        local keys = {}

        for k, _ in pairs(t) do
                keys[#keys + 1] = k
        end

        table.sort(keys)

        local i = 0

        return function ()
                if i < #keys then
                        i = i + 1

                        return keys[i], t[keys[i]]
                end
        end
end -- sorted_pairs

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic