文档章节

lua中variables和table

andrew810810
 andrew810810
发布于 2017/02/15 17:44
字数 527
阅读 15
收藏 0
Lua

reference:

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

 

3.2 – Variables

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

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

var ::= Name

Name denotes identifiers, as defined in §3.1.

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

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

Square brackets are used to index a table:

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

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

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

 

© 著作权归作者所有

共有 人打赏支持
andrew810810
粉丝 3
博文 141
码字总数 117648
作品 0
朝阳
uLua中遇到的问题

1.C#调用lua函数参数为空的问题 在C#中调用以(:)定义的函数传参时,self被第一个参数覆盖,而obj将为空 MessagePanel = {}; function MessagePanel:Test1(obj)--lua调用正常,obj为传入参数...

乐逍遥jun
2016/02/22
12
0
OpenResty 1.7.10.2 正式发布,Web 应用服务器

OpenResty 1.7.10.2 正式发布,更新内容: bugfix: ./configure: fixed the option by disabling NginxDevelKit automatically; also automatically disable the EncryptedSessionNginxModu......

oschina
2015/07/04
2.7K
12
用LuaBridge为Lua绑定C/C++对象

最近为了总结Lua绑定C/C++对象的各种方法、第三方库和原理,学习了LuaBridge库为Lua绑定C/C++对象,下面是学习笔记,实质是对该库的Reference Manual基本上翻译了一遍,学习过程中测试代码,...

shezjl
2015/10/26
0
0
openresty获取请求参数

获取url参数 ngx.var.argxx与ngx.req.geturi_args["xx"]两者都是为了获取请求uri中的参数,例如 ?strider=1为了获取输入参数strider,以下两种方法都可以: 差别在于,当请求uri中有多个同名...

go4it
2016/12/26
6
0
OpenResty 1.4.3.1 发布,Web 应用服务器

OpenResty 1.4.3.1 发布,OpenResty (也称为 ngxopenresty)是一个全功能的 Web 应用服务器。它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。 这是一个维护版...

oschina
2013/10/30
1K
8

没有更多内容

加载失败,请刷新页面

加载更多

下一页

jetbrains系产品IDEA:mac上面提示快捷键设置

原因 由于Mac上面的Ctrl+空格变成输入法切换的快捷键,在使用IDEA的过程中,代码提示很不方便,需要使用option+/这种传统eclipse上面的代码提示快捷键作为主要快捷键。 怎么修改? 移除【opt...

亚林瓜子
30分钟前
0
0
Exclipse 输出结果时换行

System.out.println(f1 + "\n" + d1 + "\n" + d2);

笑丶笑
30分钟前
1
0
怎样治疗标签不能触发onblur事件

I realize this was over a year ago, but it showed up for me in Google while trying to solve this same issue. It seems Chrome does not consider some elements, like body and ancho......

Weijuer
33分钟前
0
0
vue常见库安装

移动设备上的浏览器默认会在用户点击屏幕大约延迟300毫秒后才会触发点击事件,这是为了检查用户是否在做双击。为了能够立即响应用户的点击事件,才有了FastClick。 安装fastclick npm insta...

林夏夕
35分钟前
0
0
kafka 教程(三) kafka Java API 编程

下午写

MrPei
36分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部