开发者

New to Lua - Table operation

开发者 https://www.devze.com 2023-01-05 16:07 出处:网络
Can someone explain this to me?I have figured it out through this tutorial that this is known as a table. Coming from a C/C++ back开发者_如何学Goground, can someone explain how this works (I am trying

Can someone explain this to me? I have figured it out through this tutorial that this is known as a table. Coming from a C/C++ back开发者_如何学Goground, can someone explain how this works (I am trying to understand some existing Lua code)?

config = {
  devices = {
    C56    = "/dev/ttyS2",
    ELTRA  = "/dev/ttyS3",
--  MICORE = "/dev/ttyS4",
    HID    = "/dev/ttyS1",

    KEYCARD = {
  --  [6] = { tty="/dev/ttyS1", speed=9600 },
      [7] = { tty="/dev/ttyS4", speed=9600 },
    },

  },
}

Is it a config table, consisting of a device table but then there is a KEYCARD table? What are C56 and ELTRA called in Lua? Are they fields?


A table in Lua is just an untyped map, like Javascript objects or Python dictionaries. The table associates one value (like "devices" or 6) with another value (like "/dev/ttyS2"). The value could be another table. Tables are used to create objects, maps, and arrays.

In your example, the config variable references a table. That table has one element, "devices", whose value is another table. That table has 5 elements. Four of those elements ("C56", "ELTRA", "MICORE", and "HID") have strings as their values. The fifth element ("KEYCARD") has a table as its value. That table has two elements (6, 7) whose values are other tables (each of two elements).


You have a config table two subtables within it, devices and Keycard, which is a subtable of devices. It's been a while since I used Lua, but to access, for example ELTRA, you'd type Config.devices.ELTRA and to access the 7 keycard you type Config.devices.KEYCARD[7] To get at the speed of the keycard, you could do speed = Config.devices.KEYCARD[7].speed

0

精彩评论

暂无评论...
验证码 换一张
取 消