Freenet Alliance Society (F.A.S)
Welcome to F.A.S Forum
How to flatten a table in Lua 5.3+ 21317717
Freenet Alliance Society (F.A.S)
Welcome to F.A.S Forum
How to flatten a table in Lua 5.3+ 21317717

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+ Empty How to flatten a table in Lua 5.3+

Post by J e F F 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:
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
J e F F
J e F F
New Member
New Member

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

Back to top Go down

How to flatten a table in Lua 5.3+ Empty Re: How to flatten a table in Lua 5.3+

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

keep it up
KingQueenRJ
KingQueenRJ
Administrator
Administrator

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

https://pinoynet99.forumotion.com

Back to top Go down

How to flatten a table in Lua 5.3+ Empty Re: How to flatten a table in Lua 5.3+

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

Keep it up...
Vampire
Vampire
Administrator
Administrator

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

https://pinoynet99.forumotion.com

Back to top Go down

How to flatten a table in Lua 5.3+ Empty Re: How to flatten a table in Lua 5.3+

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

keep it up (3)
Sherlock-
Sherlock-
Banned

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

Back to top Go down

How to flatten a table in Lua 5.3+ Empty Re: How to flatten a table in Lua 5.3+

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

keep it right
AhoGrill
AhoGrill
Journeyman
Journeyman

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

Back to top Go down

How to flatten a table in Lua 5.3+ Empty Re: How to flatten a table in Lua 5.3+

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

Nice on up
Fubu-chan
Fubu-chan
Honorary Poster
Honorary Poster

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

Back to top Go down

How to flatten a table in Lua 5.3+ Empty 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


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