Lua传递多维数组

时间:2017-08-01 22:28:45

标签: function multidimensional-array lua parameter-passing

嗨,我是一个相当新的Lua程序员,我正在尝试将参数传递给多维数组,但我一直得到一个nil值错误。

我有一个多维数组:

local transform = {
  forward = {
    distance = 1,
    swing = robot.swing,
    move = robot.forward
  },
  left = {
    distance = 1,
    swing = robot.swing,
    move = robot.forward
  },
  down = {
    distance = 1,
    swing = robot.swingDown,
    move = robot.down
  }
}

我想尝试传递数组的部分内容:

function testFunc(transform, direction)
  print(transform.direction.distance)
end

myFunc(transform, forward)

1 个答案:

答案 0 :(得分:1)

表达式transform.directiontransform中查找其键为" direction"的值。如果您想要其键值为direction变量的值,请改用transform[direction]

function testFunc(transform, direction)
  print(transform[direction].distance)
end