脚本语言 是在载入的时候进行解释和编译 ,在调用的时候处理,而不是预编译;
LUA和程序开发语言配合使用时,lua一般作为特定项目的框架语言;
for index=1,4 do
print (“li:”,index)
end
– 单行注释
–[[
–]] 多行注释

lua中有5中变量类型:nil,Boolean,string,Number,table
nil:用来表示变量还有被赋值,如果给一个变量nil,那么实际上表示删除该变量;
Boolean:有两种值true,false,
string:
print(“8”+8) 16
print(“8+8”) 8+8
print(“hello world”+8) ERROR
Number:代表双精度浮点数,lua没有整数类型
table:
…………

局部变量和全局变量
lua变量默认都是全局的,但是尽量使用局部变量会好一些
用local local index=3

¥¥ 运算符

-= not equal
<
<= >=

两个table比较 只有连个table是同一个对象的时候 才相同。tableA={1,2,3} tableB={1,2,3} 是不相同的
and or not
nil被逻辑运算符当作false

控制结构

所有的控制结构以end作为结束
if while repeat
while 条件 do
结构
end

repeat
循环
until 条件

¥¥ 标准函数
assert(myvalue)() 可以把一句语句放入string中 然后用loadstring 或者assert来执行

dofile();

math.floor();向下取整(舍去小数部分)
math.random();随机产生一个0-1直接的数字
字符处理
string.char();可以根据ASCII编码返回传入参数对应的字符
string.len();
string.sub(myString,start,end);选取字符串的指定部分;
string.format();
string.find(sourceString,findString);返回的是位置 sStart,sEnd = string.find(myString,”ls”);

字符格式
exp:
myString = “The price is $17.50.”
filter = “%s%d.%d%s”
print(string.sub(myString,string.find(myString,filter)))
%d 表示所有数字 %D 表示非数字
%a 字母 %c 控制符
%l 小写字母 %p 标点符号
%u 大写字母 %w 字母数字
%x 十六进制 %z 用0表示的字符

string.gsub(sourceString,pattern,replaceString);

返回一个字符串,sourceString 字符中满足pattern格式的字符都会被替换成replaceString参数的值
可在后在加入一个参数 用于指定替换的次数

string.gfind(souceString,pattern);

遍历一个字符串,一旦查找到符合指定格式的字符串就返回该子串,在解析游戏数据是非常有用的
exp:

myString = “This is my rather long string” 
 print(mystring) 
 counter = 1; 
 for myWord in string.gfind(myString,”%a+”) do 
 print(string.format(“Word#%d:%s”,counter,myWord)) 
 counter = counter+1 
 end

table 数据结构
table.getn(myTable) 返回table中元素的个数
table.insert(myTable,position,value) 如果没有指定位置则自动添加到table的末尾,在插入完后 自动重新索引
table.remove(myTable,position) 如果没有指定位置则默认删除最后一个元素
table 引用 可以mytable.name = “dqwdqw”
多维table
pairs()遍历整个table 返回索引和对应的元素的值,并且可以遍历非数字索引

myNames = {“ls”,”ms”} 
 for index,value in pairs(myNames) do 
 print(index,value) 
 end*****I/O 
 myFile = io.open(“test_data.lua”,”w”) 
 myFile:write(……)