-- Example 1 -- First Program. -- Classic hello program. print("hello") -------- Output ------ hello -- 例子 1 -- 第一个程序. -- 经典的 hello 程序 -- Example 2 -- Comments. -- Single line comments in Lua start with double hyphen. --[[ Multiple line comments start with double hyphen and two square brackets. and end with two square brackets. ]] -- And of course this example produces no -- output, since it's all comments! -------- Output ------ -- 例子 2 -- 注释 -- 在语句前加2个连号即是单行注释 --[[ 在前面加2个连号和两个方括号, 在后面加两个方括号即是多行注释]] -- 当然这个例子程序不会有任何输出,因为都是注释! -- Example 3 -- Variables. -- Variables hold values which have types, variables dont't have types. a=1 b="abc" c={} d=print print(type(a)) print(type(b)) print(type(c)) print(type(d)) ------ Output ------- number string table function -- 例子 3 -- 变量 -- 变量持有拥有类型的值,变量本身没有类型 ------ Output ------- -- Example 4 -- Variable name. -- Variable names consist of letters, digits and underscores. -- They cannot start with a digit. one_two_3 = 123 -- is valid variable name -- 1_two_3 is not a valid variable name. ------ Output ------- -- 例子 4 -- 变量名 -- 变量名由字母,数字和下划线组成 -- 它们不能以数字开头 one_two_3 = 123 -- 是合法的变量名 -- 1_two_3 不是合法的变量名 -- Example 5 -- More Variable names. -- The underscore is typically used to start special values. -- like _VERSION in Lua. print(_VERSION) -- So don't used variables that start with _, -- but a single underscore _ is often used as a -- dummy variable. ------- Output ------- Lua 5.1 -- 例子 5 -- 变量名称续集 -- 要开始一个特殊的变量,通常以下划线开头 -- 例如Lua中的_VERSION print(_VERSION) -- 所以不要使用下划线来命名变量 -- 但是以一个下划线命名的变量通常用来充当哑变量 -- Example 6 -- Case Sensitive -- Lua is case sensitive so all variable names & keywords -- must be in correct case. ab=1 Ab=2 AB=3 print(ab,Ab,AB) ------ Output ------ 1 2 3 -- 例子6 -- 大小写的区分 -- Lua是区分大小写的,所以所有的变量名和关键字都必须正确的大小写 -- Example 7 -- Keywords -- Lua reserved words are: and, break, do, else, elseif, -- end, false, for, function, if, in, local, nil, not, or, -- repeat, return, then, true, until, while. -- Keywords cannot be used for variable names, -- 'and' is a keyword, but AND is not, so it is a legal variable name. AND=3 print(AND) ------ Output ------ 3 -- 例子 7 -- 关键字 -- Lua 保留字如下:and, break, do, else, elseif, -- end, false, for, function, if, in, local, nil, not, or, -- repeat, return, then, true, until, while. -- 关键字不能作为变量名 -- ‘and'是关键字,但是AND不是,所以这是个合法的变量名 -- Example 8 -- Strings. a="single 'quoted' string and double \"quoted\" string inside" b='single \'quoted\' string and double "quoted" string inside' c = [[ multiple line with 'single' and "double" quoted strings inside.]] print(a) print(b) print(c) ------- Output ------ single 'quoted' string and double "quoted" string inside single 'quoted' string and double "quoted" string inside multiple line with 'single' and "double" quoted strings inside. -- 例子 8 -- 字符串 不译。如果字符串用单引号包裹,在串中的单引号需要转义;用双引号同理;[[ 多行注释 ]] -- Example 9 -- Assignments -- Multiple assignments are valid. -- var1,var2=var3,var4 a,b,c,d,e = 1, 2, "three", "four", 5 print(a,b,c,d,e) ------ Output ------ 1 2 three four 5 -- 例子9 -- 赋值 -- 多重赋值是合法的 -- Example 10 -- More Assignments. -- Multiple assignments allows one line to swap two variables. print(a,b) a,b=b,a print(a,b) ------ Output ------ -- 例子 10 -- 赋值续集 -- 多重赋值允许使用一行语句交换2个变量的值