lua中variables和table

reference:html

http://www.lua.org/manual/5.3/manual.htmlexpress

 

3.2 – Variables

Variables are places that store values. There are three kinds of variables in Lua: global variables, local variables, and table fields.less

A single name can denote a global variable or a local variable (or a function's formal parameter, which is a particular kind of local variable):ide

var ::= Name

Name denotes identifiers, as defined in §3.1.ui

Any variable name is assumed to be global unless explicitly declared as a local (see §3.3.7). Local variables are lexically scoped: local variables can be freely accessed by functions defined inside their scope (see §3.5).this

Before the first assignment to a variable, its value is nil.lua

Square brackets are used to index a table:spa

var ::= prefixexp ‘[’ exp ‘]

The meaning of accesses to table fields can be changed via metatables. An access to an indexed variable t[i] is equivalent to a call gettable_event(t,i). (See §2.4 for a complete description of the gettable_event function. This function is not defined or callable in Lua. We use it here only for explanatory purposes.)code

The syntax var.Name is just syntactic sugar for var["Name"]:orm

var ::= prefixexp ‘.’ Name

An access to a global variable x is equivalent to _ENV.x. Due to the way that chunks are compiled, _ENV is never a global name (see §2.2).

 

 

3.4.9 – Table Constructors

Table constructors are expressions that create tables. Every time a constructor is evaluated, a new table is created. A constructor can be used to create an empty table or to create a table and initialize some of its fields. The general syntax for constructors is

tableconstructor ::= ‘{’ [fieldlist] ‘}’
	fieldlist ::= field {fieldsep field} [fieldsep]
	field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
	fieldsep ::= ‘,’ | ‘;

Each field of the form [exp1] = exp2 adds to the new table an entry with key exp1 and value exp2. A field of the form name = exp is equivalent to ["name"] = exp. Finally, fields of the form exp are equivalent to [i] = exp, where i are consecutive integers starting with 1. Fields in the other formats do not affect this counting. For example,

a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }

is equivalent to

do
       local t = {}
       t[f(1)] = g
       t[1] = "x"         -- 1st exp
       t[2] = "y"         -- 2nd exp
       t.x = 1            -- t["x"] = 1
       t[3] = f(x)        -- 3rd exp
       t[30] = 23
       t[4] = 45          -- 4th exp
       a = t
     end

The order of the assignments in a constructor is undefined. (This order would be relevant only when there are repeated keys.)

If the last field in the list has the form exp and the expression is a function call or a vararg expression, then all values returned by this expression enter the list consecutively (see §3.4.10).

The field list can have an optional trailing separator, as a convenience for machine-generated code.

 

 

示例1 variables:

local res="352*288"
print(res, res[1])

運行結果:

352*288	nil

 

示例2 table:

local res={"352*288"}
for j=1,#res do  
  print(res, res[1])
  local res_h, res_v = string.match(res[j],"([^*]+)*([^*]+)")
  print(res_h, res_v)
end

運行結果:

table: 0x1fed560	352*288
352	288
相關文章
相關標籤/搜索