Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New to Lua - Table operation

Tags:

lua

lua-table

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++ background, 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?


1 Answers

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).

like image 167
Karmastan Avatar answered Mar 23 '26 08:03

Karmastan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!