Freenet Alliance Society (F.A.S)
Welcome to F.A.S Forum

How to flatten a table in Lua 5.3+

View previous topic View next topic Go down

How to flatten a table in Lua 5.3+

Post by J e F F on Tue Sep 05, 2017 9:58 pm

This is a code I used for our game. It flattens a table and returns an associative table that maps a string key to its value.
Example:
For example, for this table
Code:

local assets = {
  images = {
    guilty_kiss = 'res/images/gk01.png';
    nico = 'res/images/nico_yazawa.png';
  };
  shaders = {
    vert = {
      simple = 'res/shaders/simple.vert.glsl';
    };
    frag = {
      simple = 'res/shaders/simple.frag.glsl';
    }
  };
}

You'll get something as if you did the following:
Code:

local flattened_table = {
  assets.images.guilty_kiss = 'res/images/gk01.png';
  assets.images.nico = 'res/images/nico_yazawa.png';
  assets.shaders.vert.simple = 'res/shaders/simple.vert.glsl';
  assets.shaders.frag.simple = 'res/shaders/simple.frag.glsl';
}
So for what's the purpose of this thing? We used it as resource handles ^^
Kita mo yung resources sa log ng Counter Strike? Parang ganun.

So eto na siya:
Code:

local flatten
do
  local function _flatten(r, pr, ks, it, t, k, v)
    if v == nil then
      -- we're done
      if #ks == 0 then return r end
      -- unpack saved state (for ver 5.3 below just 'unpack')
      it, t, k, v = table.unpack(ks[#ks])
      ks[#ks] = nil
      pr[#pr] = nil
    else
      -- push the key as a prefix
      pr[#pr+1] = k
      if type(v) ~= 'table' then
        -- if it isnt a table, add to our results
        r[table.concat(pr, '.')] = tostring(v)
        pr[#pr] = nil
      else
        -- otherwise if it's a table save state
        ks[#ks + 1] = {it, t, k, v}
        it, t, k = pairs(v)
      end
    end

    return _flatten(r, pr, ks, it, t, it(t, k))
  end
  
  flatten = function(root)
    local it, t, v = pairs(root)
    return _flatten({}, {}, {}, it, t, it(t, v))
  end
    
end

Tell me kung may ganitong function na or optimal method. I made it in a few minutes and honestly hindi pa na u-unit test. However no bugs have been discovered yet that came from this function so far xD
avatar
J e F F
New Member
New Member

Posts : 12
Trophy : 41
Join date : 2017-09-05
Age : 22
Location : Mexico

Back to top Go down

Re: How to flatten a table in Lua 5.3+

Post by KingQueenRJ on Tue Sep 05, 2017 9:58 pm

keep it up


[You must be registered and logged in to see this link.] | [You must be registered and logged in to see this link.] | [You must be registered and logged in to see this link.] [You must be registered and logged in to see this link.]|[You must be registered and logged in to see this link.][You must be registered and logged in to see this link.][You must be registered and logged in to see this link.] [You must be registered and logged in to see this link.]
KingQueenRJ Administrator of FreeNet Allience Society
avatar
KingQueenRJ
Administrator
Administrator

Posts : 214
Trophy : 1501
Join date : 2017-09-02
Location : lamao,liloy, Zamboanga Del norte

http://pinoynet99.forumotion.com

Back to top Go down

Re: How to flatten a table in Lua 5.3+

Post by Vampire on Wed Sep 06, 2017 8:14 am

Keep it up...


Announcement : cheers:  : [You must be registered and logged in to see this link.] is now open. Reminder Use Dektop mode to have a full Feature the Pc Version Is Cool.... Razz !!!
[You must be registered and logged in to see this link.]
avatar
Vampire
Administrator
Administrator

Posts : 438
Trophy : 58654049
Join date : 2017-08-31
Location : F.A.S

http://pinoynet99.forumotion.com

Back to top Go down

Re: How to flatten a table in Lua 5.3+

Post by Sherlock- on Wed Sep 06, 2017 5:15 pm

keep it up (3)
avatar
Sherlock-
Banned

Posts : 42
Trophy : 52
Join date : 2017-09-02
Location : 221B Baker Street

Back to top Go down

Re: How to flatten a table in Lua 5.3+

Post by AhoGrill on Wed Sep 06, 2017 5:25 pm

keep it right
avatar
AhoGrill
Journeyman
Journeyman

Posts : 50
Trophy : 112
Join date : 2017-09-02
Age : 12
Location : I'm Aho so I don't know

Back to top Go down

Re: How to flatten a table in Lua 5.3+

Post by Fubu-chan on Wed Sep 06, 2017 9:37 pm

Nice on up
avatar
Fubu-chan
Honorary Poster
Honorary Poster

Posts : 163
Trophy : 237
Join date : 2017-09-02
Age : 17
Location : bikini bottom

Back to top Go down

Re: How to flatten a table in Lua 5.3+

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum