lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On May 17, 2014, at 7:58 PM, Thiago L. <fakedme@gmail.com> wrote:

> And what if I want to do something like table?something[someVariable]?somethingElse

Suit yourself:

local function get( aTarget, ... )
  local aCall = function( aKey ) aTarget = aTarget[ aKey ] end

  for anIndex = 1, select( '#', ... ) do
    local aKey = select( anIndex, ... )

    if not pcall( aCall, aKey ) then
      break
    end
  end

  return aTarget
end

local aTable = { { 1, { 1, 2, { 1, 2, 3, { 1, 2, 3, 4, { 1, 2, 3, 4, 5, { 1, 2, 3, 4, 5, 6, { 1, 2, 3, 4, 5, 6, 7, { 1, 2, 3, 4, 5, 6, 7, 8, 9 } } } } } } } } }

print( get( aTable, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ) )