Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference lua table member from table member?

Tags:

lua

i have a table in lua:

enUS = {
   LOCALE_STHOUSANDS = ",",  --Thousands separator e.g. comma

   patNumber = "%d+["..LOCALE_STHOUSANDS.."%d]*", --regex to find a number

   ["PreScanPatterns"] = {
      ["^("..patNumber..") Armor$"] = "ARMOR",
   }
}

So you see there is a whole chain of self-references in this table:

  • LOCAL_STHOUSANDS
    • patNumber
      • ["^("..patNumber..") Armor$"]

How can i perform self-referencing in an lua table?


What i don't want to do is have to hard-replace the values; there are hundreds of references:

enUS = {
   LOCALE_STHOUSANDS = ",",  --Thousands separator e.g. comma

   patNumber = "%d+[,%d]*", --regex to find a number

   ["PreScanPatterns"] = {
      ["^(%d+[,%d]*) Armor$"] = "ARMOR",
   }
}
like image 890
Ian Boyd Avatar asked Aug 08 '12 00:08

Ian Boyd


People also ask

How do I unpack a table in Lua?

When we want to return multiple values from a table, we make use of the table. unpack() function. It takes a list and returns multiple values.

How do I use table insert in Lua?

In Lua, the table library provides functions to insert elements into the table. The insert function usually takes two arguments, the first argument is usually the name of the table from which we want to insert the element to, and the second argument is the element that we want to insert.

How does a table work in Lua?

Tables are the only data structure available in Lua that helps us create different types like arrays and dictionaries. Lua uses associative arrays and which can be indexed with not only numbers but also with strings except nil. Tables have no fixed size and can grow based on our need.

What is a table value Lua?

Tables are the only "container" type in Lua. They are associative arrays ([1]), which means they store a set of key/value pairs. In a Key/Value pair you can store a value under a key and then later retrieve the value using that key.


1 Answers

How can i perform self-referencing in an lua table?

You don't.

Lua is not C. Until the table is constructed, none of the table entries exist. Because the table itself doesn't exist yet. Therefore, you can't have one entry in a table constructor reference another entry in a table that doesn't exist.

If you want to cut down on repeated typing, then you should use local variables and do/end blocks:

do
  local temp_thousands_separator = ","
  local temp_number_pattern = "%d+["..LOCALE_STHOUSANDS.."%d]*"

  enUS = {
   LOCALE_STHOUSANDS = temp_thousands_separator,  --Thousands separator e.g. comma

   patNumber = "%d+["..temp_thousands_separator.."%d]*", --regex to find a number

   ["PreScanPatterns"] = {
      ["^("..temp_number_pattern..") Armor$"] = "ARMOR",
   }
  }
end

The do/end block is there so that the temporary variables don't exist outside of the table creation code.

Alternatively, you can do the construction in stages:

  enUS = {}
  enUS.LOCALE_STHOUSANDS = ",",  --Thousands separator e.g. comma

  enUS.patNumber = "%d+["..enUS.LOCALE_STHOUSANDS.."%d]*", --regex to find a number

  enUS["PreScanPatterns"] = {
      ["^("..enUS.patNumber..") Armor$"] = "ARMOR",
   }
like image 103
Nicol Bolas Avatar answered Sep 22 '22 12:09

Nicol Bolas