-- test lua: for learning lua grammar

-- line comment
--[[
block comment
]]-- -- print hello world
print('Hello World\n') -- control structure
-- if
if 1+1 == 2 then print('1+1=2') end
if 1+1 == 2 then
print('1+1=2')
elseif 1+1 == 3 then
print('1+1=3')
else
print('1+1=?')
end -- while
local n = 2
while n > 0 do
print('while ' .. n)
n = n - 1
end -- repeat
n = 2
repeat
print('repeat ' .. n)
n = n - 1
until n <= 0 -- for
for i = 1, 2, 1 do
print('for ' .. i)
end -- for array
arr = {1, 2}
for i, v in ipairs(arr) do
print('arr ' .. i .. ' ' .. v)
end -- for table
t = {
name = 'adfan',
age = 20,
[2] = 30,
}
for k, v in pairs(t) do
print('table ' .. k .. ' = ' .. v)
end -- assign
a, b, c, d = 1, 2, 3, 4
print(a, b, c, d)
-- exchange a, b
a, b = b, a
print(a, b) -- math
print(2 ^ 4) -- 2 power 4 = 16 -- compare
print(1 ~= 2) -- not equal -- logic
--[[
Note: only false or nil are false, others are true (0 is alse true!)
a and b: if a is false, return a; else return b (return the first false)
a or b: if a is true, return a; else return b (return the first true)
]]--
print(true and 5) -- 5
print(false and true) -- false
print(true and 5 and 1) -- 1 print(false or 0) -- 0
print(nil or 1) -- 1 print(not nil) -- true
print(not 0) -- false: as 0 is true a, b, c = 1, nil, 3
x = a and b or c -- not means a ? b : c, as when b is false, x equals c..
print(x)
x = x or a -- means if not x then x = v end
print(x) -- opr inc order:
--[[
or
and
< > <= >= ~= ==
..(string plus)
+ -
* / %
not #(get length) -
^
]]-- -- var type
print('type nil ' .. type(nil))
print('type true ' .. type(true))
print('type 1 ' .. type(1))
print('type str ' .. type('adfan'))
print('type table ' .. type({1, 2, 3}))
print('type function ' .. type(function () end))
-- user data -- local means local var, without local means global var
-- table
t = {
10, -- means [1] = 10
[100] = 40,
John = {
age = 27,
gender = male,
},
20 -- means [2] = 20
} -- function
function add(a, b)
return a + b
end function sum(a, b, ...)
c, d = ...
print(...)
print(a, b, c, d)
return 14, 13, 12, 11
end
a, b, c, d = sum(1, 2, 3, 4)
print(a, b, c, d)

lua——基础语法的更多相关文章

  1. Lua基础语法讲解

    Lua 是什么? Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. Lua 是巴西里约热内卢天主教大学( ...

  2. Lua - 基础语法

    Hello World 交互式编程 Lua 交互式编程模式可以通过命令 lua -i 或 lua 来启用: [huey@huey-K42JE lua]$ lua Lua 5.1.4 Copyright ...

  3. (一)lua基础语法

    1.从hellowrold开始 --语法和Python比较类似,直接像Python一样使用print即可 --这里我可以直接写中文,显然被当成了注释.在lua中,两个-表示注释 --[[ 这种形式可以 ...

  4. Nginx详解二十二:Nginx深度学习篇之Lua解释器安装及基础语法

    解释器 Lua:Lua是一个简洁.轻量.可扩展的脚本语言 Nginx+Lua优势充分的结合Nginx的并发处理epoll优势的Lua的轻量实现简单的功能切高并发的场景 安装Lua 1.安装解释器:yu ...

  5. 大体了解Lua的语法

    Lua 的语法比较简单,学习起来也比较省力,但功能却并不弱. 在Lua中,一切都是变量,除了关键字.请记住这句话. I. 首先是注释 写一个程序,总是少不了注释的. 在Lua中,你可以使用单行注释和多 ...

  6. Lua基础 函数(一)

    转自: http://blog.csdn.net/wzzfeitian/article/details/8653101 在Lua中,函数是对语句和表达式进行抽象的主要方法.既可以用来处理一些特殊的工作 ...

  7. Python学习的个人笔记(基础语法)

    Python学习的个人笔记 题外话: 我是一个大二的计算机系的学生,这份python学习个人笔记是趁寒假这一周在慕课网,w3cschool,还有借鉴了一些博客,资料整理出来的,用于自己方便的时候查阅, ...

  8. python基础语法及知识点总结

    本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...

  9. Cocos2d-x 脚本语言Lua基本语法

    Cocos2d-x 脚本语言Lua基本语法 前面一篇博客对Lua这门小巧的语言进行了简单的介绍.本篇博客来给大家略微讲一下Lua的语法.不会长篇累牍得把Lua的全部语法都讲一遍,这里通过下面几点来讲L ...

随机推荐

  1. 匈牙利算法 - Luogu 1963 变换序列

    P1963 变换序列 题目描述 对于N个整数0,1,-,N-1,一个变换序列T可以将i变成Ti,其中:Ti∈{0,1,-,N-1}且 {Ti}={0,1,-,N-1}. x,y∈{0,1,-,N-1} ...

  2. 如何理解redo和undo的作用

    目录 如何理解redo和undo的作用 redo undo UNDO和REDO的区别 如何理解redo和undo的作用 redo 重做日志(redo)包含所有数据产生的历史改变记录,是oracle在线 ...

  3. Leetcode3--->无重复字符的最长子串长度

    题目:给定一个字符串string,找出string中无重复字符的最长子串. 举例: Given "abcabcbb", the answer is "abc", ...

  4. Python面试题(练习一)

    1.Python的可变类型和不可变类型? 可变类型:list.dict(列表和字典) 不可变类型:数字.字符串.元组 2.求结果: v = dict.fromkeys(['k1','k2'],[]) ...

  5. tensorflow——MNIST机器学习入门

    将这里的代码在项目中执行下载并安装数据集. 执行下面代码,训练.并评估模型: # _*_coding:utf-8_*_ import inputdata mnist = inputdata.read_ ...

  6. Uiautomator ---(1) 封装代码

    http://www.cnblogs.com/by-dream/p/4996000.html  上面是别人的写法 我自己的写法: package qq.test; import android.con ...

  7. [uiautomator篇] [4] 运行成功的日志打印---最后写一个脚本来实现

    Testing started at 18:23 ... 05/10 18:23:01: Launching ChangeTextBehaviorTestNo apk changes detected ...

  8. learn资料

    老陈的CSDN博客: http://blog.csdn.net/qq_35587839 1.memcache 和 memcached的区别:http://www.phpweblog.net/fuyon ...

  9. 关于 lambda expression 返回值的类型转换

    lambda expression(lambda 表达式,$\lambda$ 表达式) 是 C++ 11 引入的特性. 一般而言,lambda 表达式的返回值类型可不指定,而由返回值推断. 需要注意的 ...

  10. UVa——1593Alignment of Code(string重定向+vector数组)

    UVA - 1593 Alignment of Code Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & ...