Quantcast
Channel: How to reference lua table member from table member? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by Nicol Bolas for How to reference lua table member from table member?

$
0
0

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",   }

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>