首页 > 其他 > 详细

小清新-lua

时间:2015-10-07 21:36:10      阅读:198      评论:0      收藏:0      [点我收藏+]

http://files.luaforge.net/releases/luaforwindows/luaforwindows/5.1.4-35/LuaForWindows_v5.1.4-35.exe

安装lua , 到最后一步的时候 ,勾选上 run example  看完这些就入门了,还是很好的



-- Example 14   -- Tables.
-- Simple table creation.

a={} -- {} creates an empty table
b={1,2,3} -- creates a table containing numbers 1,2,3
c={"a","b","c"} -- creates a table containing strings a,b,c
print(a,b,c) -- tables don‘t print directly, we‘ll get back to this!!


-------- Output ------

table: 006BB5B8 table: 006BB680 table: 006BB6F8


请问这一段怎么理解呢。是不是 lua 里面table就是一个对象了,打印的是内存地址呢 技术分享



address={} -- empty address
address.Street="Wyman Street"
address.StreetNumber=360
address.AptNumber="2a"
address.City="Watertown"
address.State="Vermont"
address.County="USA"

print(address.StreetNumber, address["AptNumber"])



-------- Output ------

360     2a


下面这一段 应该和 a ? b : c 是一个意思


-- value = test and x or y

a=1
b=(a==1) and "one" or "not one"
print(b)

-- is equivalent to
a=1
if a==1 then
    b = "one"
else
    b = "not one"
end
print(b)


lua中 值的 不等于 ~=


a=1
while a~=5 do -- Lua uses ~= to mean not equal
    a=a+1
    io.write(a.." ")
end


-------- Output ------

2 3 4 5



lua中的 do-while 


a=0
repeat
    a=a+1
    print(a)
until a==5


-------- Output ------

1
2
3
4
5



for 循环,1,4 结果的猜测,1<6 打印1,1+3 =>4 ,4<6 打印4 ,4+3 >6跳出循环


-- Numeric iteration form.

-- Count from 1 to 4 by 1.
for a=1,4 do io.write(a) end

print()

-- Count from 1 to 6 by 3.
for a=1,6,3 do io.write(a) end


-------- Output ------

1234
14
Press ‘Enter‘ key for next example




下面这个还没有猜测出


-- Sequential iteration form.

for key,value in pairs({1,2,3,4}) do print(key, value) end


-------- Output ------

1       1
2       2
3       3
4       4



a={1,2,3,4,"five","elephant", "mouse"}

for i,v in pairs(a) do print(i,v) end


-------- Output ------

1       1
2       2
3       3
4       4
5       five
6       elephant
7       mouse



lua while break


-- break is used to exit a loop.

a=0
while true do
    a=a+1
    if a==10 then
        break
    end
end

print(a)


-------- Output ------

10



lua的 function 和 JavaScript很像


-- Define a function without parameters or return value.
function myFirstLuaFunction()
    print("My first lua function was called")
end

-- Call myFirstLuaFunction.
myFirstLuaFunction()


-------- Output ------

My first lua function was called

Press ‘Enter‘ key for next example



带返回值的 function


-- Define a function with a return value.
function mySecondLuaFunction()
    return "string from my second function"
end

-- Call function returning a value.
a=mySecondLuaFunction("string")
print(a)


-------- Output ------

string from my second function




还有一种就是 可以返回多个 返回值,比较有趣

function a(a,b,c)

    return a,b,c,"func"

end


lua的本地变量概念,如果和全局变量是一个变量,要在function内声明为local 变量


-- All variables are global in scope by default.

b="global"

-- To make local variables you must put the keyword ‘local‘ in front.
function myfunc()
    local b=" local variable"
    a="global variable"
    print(a,b)
end

myfunc()
print(a,b)


-------- Output ------

global variable  local variable
global variable global



Lua的 string format..可别的语言差不多


-- An implementation of printf.

function printf(fmt, ...)
    io.write(string.format(fmt, ...))
end

printf("Hello %s from %s on %s\n",
       os.getenv"USER" or "there", _VERSION, os.date())


-------- Output ------

Hello there from Lua 5.1 on 10/07/15 21:06:22



lua类库分为标准库和扩展库,简单介绍


 Standard Libraries

  Lua has standard built-in libraries for common operations in
  math, string, table, input/output & operating system facilities.

 External Libraries

  Numerous other libraries have been created: sockets, XML, profiling,
  logging, unittests, GUI toolkits, web frameworks, and many more.

]]



lua标准库——math,据说解释器不到200kb,太强大了


-- Example 32   -- Standard Libraries - math.

-- Math functions:
-- math.abs, math.acos, math.asin, math.atan, math.atan2,
-- math.ceil, math.cos, math.cosh, math.deg, math.exp, math.floor,
-- math.fmod, math.frexp, math.huge, math.ldexp, math.log, math.log10,
-- math.max, math.min, math.modf, math.pi, math.pow, math.rad,
-- math.random, math.randomseed, math.sin, math.sinh, math.sqrt,
-- math.tan, math.tanh

print(math.sqrt(9), math.pi)


-------- Output ------

3       3.1415926535898



标准库——table,感觉这个example比文档里面那一堆清晰


-- Example 34   -- Standard Libraries - table.

-- Table functions:
-- table.concat, table.insert, table.maxn, table.remove, table.sort

a={2}
table.insert(a,3);
table.insert(a,4);
table.sort(a,function(v1,v2) return v1 > v2 end)
for i,v in ipairs(a) do print(i,v) end

-------- Output ------

1       4
2       3
3       2



lua标准库——操作系统 api


-- Example 36   -- Standard Libraries - operating system facilities.

-- OS functions:
-- os.clock, os.date, os.difftime, os.execute, os.exit, os.getenv,
-- os.remove, os.rename, os.setlocale, os.time, os.tmpname

print(os.date())

-------- Output ------

10/07/15 21:13:23 



lua的gui. require("xx") 难道是 requirejs 这种写法的鼻祖 ?


-- Lua has support for external modules using the ‘require‘ function
-- INFO: A dialog will popup but it could get hidden behind the console.

require( "iuplua" )
ml = iup.multiline
    {
    expand="YES",
    value="Quit this multiline edit app to continue Tutorial!",
    border="YES"
    }
dlg = iup.dialog{ml; title="IupMultiline", size="QUARTERxQUARTER",}
dlg:show()
print("Exit GUI app to continue!")
iup.MainLoop()




大结局,官方推荐学习资料


[[

 To learn more about Lua scripting see

 Lua Tutorials: http://lua-users.org/wiki/TutorialDirectory

 "Programming in Lua" Book: http://www.inf.puc-rio.br/~roberto/pil2/

 Lua 5.1 Reference Manual:
     Start/Programs/Lua/Documentation/Lua 5.1 Reference Manual

 Examples: Start/Programs/Lua/Examples
]]


下面是我找到的几个很有用的工具书.

http://coolshell.cn/articles/10739.html

http://book.luaer.cn/

http://cloudwu.github.io/lua53doc/

http://dl2.iteye.com/upload/attachment/0052/3466/c6e246e1-7270-3220-bef5-5067eb30f7d2.pdf

有个IDE 貌似不错的样子,http://www.mydevelopersgames.com/LuaGliderInstaller.msi

小清新-lua

原文:http://my.oschina.net/httpssl/blog/513975

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!