lua相关知识笔记

print("Hellow world!!!")
--[[
--变量声明直接声明并赋值
id=1
age=23
name="逆天大神:"
sex=true
print(age,name)
print(age +id)--会打印相加后的值
print("20"+"152")--对于数字类型的字符串会相加后打印  172
if id==1 then
 print("ID为1")
 elseif id==0 then  --elseif 不能分开写,且后边必须有then,并且要再开一个else分支
 print("ID==0")
 else
 print("ID未知")

end

index=0
while index<=10 do
 index =index+1  --无法使用+= -=等操作
 end

--先执行代码,类似于c#的 do while
repeat
print (index)
  index =index -1
  until  index<=0
  print (index)

--求0-10之间偶数的和
sum=0
  repeat
   if(index%2==0) then
   sum =sum +index
   end
   index =index +1
   until index >10
print("repeat--->",sum)
--for 循环

for index =1,10 do
print ("for-->",index)
end

--方法函数
function sum(num1,num2)
return num1+num2
end
print("sum--->",sum(1,3))

print("mathf-->",math.abs(-1))
print ("mathf-->",math.max(12,21))

print("random---",math.random())

--字符串
name="Nitaindasen"
print(string.lower(name))
print(string.sub(name,1,4)) --lua 的索引都是从1开始的

--tabale
--从第一个没有key的值,默认key=1,依次类推所有的没有指定key值的值

  mytable={}
  mytable[1]=0
  mytable["xaiosen"]="suai"
  print("mytable--->",mytable["xaiosen"])
  print(mytable.xaiosen)

  mytable={1,2,3,4,45,5}
  print("数组形式的索引--->",mytable[1])
  print(table.getn(mytable))

  for index=1,table.getn(mytable) do
     print(mytable[index])
	end

print(table.concat(mytable)) --讲表中数据连接成字符串
print (table.insert(mytable,2,90))
print ("insert--->",table .concat(mytable))

--实现面向对象
 people={}
 people.name="小森"
 people.sex=1
 people.money=1000000
 local this=people
 people.Eat=function()
   print(people.name,"吃饭了")
   end
function people.Play()
print ("玩了")
this.Eat()

end

people.Play()



--声明为local后是局部方法
local function funcname(arg1,arg2)
 print ("参数为--->",arg1,arg2)
 return arg1+arg2,arg1
 end
 print("输出方法返回值--->", funcname(12,23)) --输出方法返回值--->	35	12

--函数可以作为数据赋值,也可以作为参数传递
temp=funcname
print (temp(23,24))--47	23

myprint=function (arg2) --此处为匿名函数
print ("mypriint参数——————>",arg2)
end
myprint(432)

function arg(argfunc) --函数可以作为参数
 print (argfunc(1,2));
 end
 arg(funcname)

 --可变参数 (个数) ...
 function test(...)
  -- print (arg) --arg 是内置的,会吧参数封装成一个表 最后一个值是个数
  --print(arg[1])--第一个参数.
  local arg={...}--自定义,是不带个数的
  res=0;
  for k,v in pairs(arg) do --key ,value
   res =res +v;
   print(res)
   --#arg 获取个数  #""获取字符串的长度
   print(#arg)
  end
end
test(1,2)
--test(12,13,14)
 ]]--

--运算符
--[[
a=10
b=20
print ("运算符-->+",a+b)
print ("-",a-b)
print("x",a*b)
print("/",a/b)
print(a==b)
print (a>b)
print (a~=b) --a不等于b
print (a<0 or b>0)--and or  not
print (not true)
print ("hellow".."world")--字符串的拼接
print (#"hellow")--6#获取字符串的长度
tab={1,2,21,32,21}
print (#tab)--5
tab=nil
tab={1,2,2}
print (#tab)--3
tab .key=43
tab.key2="adf"
print (#tab)--3
]]--
--[[
--转义字符  \n 换行 \r 回车 \\ 代表一个\  \" 代表"
a="hellow\n world"
print(a)
print ("姓名: \"小森 \"")--姓名: "小森 "

--数组
array={"name","xiaosen"}
for i=1 ,2 do
print(array[i]) --索引从1开始
end
for i=-2,2 do  --for循环可以为负值索引
 array[i]=i
 end
 for i=-2,2 do
 print (array[i])
end

array={{"111",12},{"232","321313"},{12,32}}
for i=1,3 do
  for j=1,2 do
  array[i][j]=i*j
  print (array[i][j])
  end
end
]]--
--迭代器  pairs就是迭代器,迭代table
--ipairs按照索引从1开始,掷遇到nil停止 适合数组
--pairs ipairs返回两个值,一个key一个value
array={"111",12,nil ,"232","321313"}
for k,v in pairs(array) do
 print (k,v)
 end

for i,j in ipairs(array) do
 print("ipars",array[i])
 end

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值