1.  Lua -i main.lua

-i 进入交互模式

-l 加载一个库

-e  “lua code” 直接在命令行执行lua code

2. 注释

-- This is a line comment
--[[ This block show 
  how to block commenting some lines
]]--

3. 数据数型

8种基本数据类型:nil, boolean, number, string, function, userdata, thread, and table.

4. String.

1)字符串值不能修改,只能生成另一个字符串。

2)可用#来获取一个字符串的length。

3)[[  双括号可以

扩起多行

长字符串]]。

4)..  是字符串连接符,不要用+

       a = "one string"
       b = string.gsub(a, "one", "another")
       print(#a) 【】 
       print b    【another string】

5)  I/O


     io.read(), we read a line from the standard input.

As write is simpler than read, we will look at it first. The io.write function simply gets an arbitrary number of string arguments and writes them to the current output file. Numbers are converted to strings following the usual conversion rules; for full control over this conversion, you should use the format function, from the string library:

    > io.write("sin (3) = ", math.sin(), "\n")
      --> sin (3) = 0.1411200080598672
    > io.write(string.format("sin (3) = %.4f\n", math.sin()))
      --> sin (3) = 0.1411 As a rule, you should use print for quick-and-dirty programs, or for debugging, and write when you need full control over your output:     > print("hello", "Lua"); print("Hi")
      --> hello   Lua
      --> Hi
    
    > io.write("hello", "Lua"); io.write("Hi", "\n")
      --> helloLuaHi

6) Pattern (^区配字符串开头,$区配字符串结尾,大写表示补集,例如 %S+ 配区一个非空字符串)

.    all characters
%a letters
%c control characters
%d digits
%l lower case letters
%p punctuation characters
%s space characters
%u upper case letters
%w alphanumeric characters
%x hexadecimal digits
%z the character with representation

‘[][]’ 方括号可以匹配任何一个括号中的模式,如果[]中第一个符号是^,表示匹配一个非本[]内字符的字符。
    ‘()()()’,圆扩号表示返回其中匹配的字符串,可以一次返回多个 
    %*^$等特殊符号,可以在前面加%转义为普通字符。

*   匹配0个或多个,越多越好
   +  匹配一个或多个,越多越好
   -   匹配0个或多个,越小越好
   ? 表示前面的字符可以有,也可以没有,本意在于忽略

5. Table

a = {}
a["x"] =  相当于 a.x =   注意: a.x   表示 a["x"] ,此时x是一个字符串,做索引。
 
a = { x=, y =} 相当于 a = {};  a.x =;  a.y =
i = ; a = {[i+] = "a", [i+] = "b", [i+] = "c"}
 
表示数组只需要用数字做为索引即可, a = {"", "", ""} 相当于 a[] = "10; a[2] = ""; a[3] = "",
Lua数组下标默认是1开始,要用0的话,可以显示的写成 a= {[] = "", "", ""}

6. if语句,for循环

if a< then 
    a =  
end
    if a<b then 
        return a 
    else 
        return b 
    end
    
    if op == "+" then
        r = a + b
    elseif op == "-" then
        r = a - b
    else
        error("invalid operation")
    end
a = {}  
for i=,[,k] do  -- k为步长,可省略不写,默认值为1,若想递减可改为-1
    a[i] = i; 
end 
while 条件 do
end
repeat
until 条件
7. 操作符
    and, or 是短路求值,即当第一个参数能决定表达式的值时,就不再去判断第二个参数。
    x = x and v, 意思是如果x非空,那么x = v,x 若为nil/fasle,x = nil/false
    x = x or v ,意思是如果x为空,那么x=v,否则 x = x
    优先级,from the higher to the lower priority:
             ^
             not  - (unary)
             *   /
             +   -
             ..
             <   >   <=  >=  ~=  ==
             and
             or
8. 变量赋值 
a, b, c = , ,        --   一次赋值多个变量
a, b, c =                --   b,c会被赋值为nil
a, b, c = , , ,    --   3会被丢掉
x, y = y, x               --   交换x与y的值
local a =        -- local 关键字用来定义局部变量,尽量多的使用局部变量是一种美德
9. 变长参数 & 泛型for
function add(...)
local s = 
for i, v in ipairs{...} do   -- i为index, v 为value
s = s +v
end 
return s
end
10. Function.
function new_counter()
    local i=
    return function()
            i = i +
               return i
           end
end

c1 = new_counter()

print(c1()) --> 1

print(c1()) --> 2

11. 协程(coroutine)
1) 协程的四种状态: 挂起(suspended), 运行(running),死亡(dead),正常(normal)
2) 协程创建后处于挂起状态,需要coroutine.resume(协程名)才能执行。
co = coroutine.create(function() print("hi") end)
print(co)  -->thread:0x8071d98
print(coroutine.status(co))  -->suspended
coroutine.resume(co)   --> hi
3) 调用coroutine.yield,可让协程挂起。
co = coroutine.create( function()
    for i = ,  do 
        print("co", i)
        coroutine.yield()
    end
end

coroutine.resume(co)  -->co 1
print(coroutine.status(co))  --> suspended
coroutine.resume(co)  -->co 2
coroutine.resume(co)  -->co 3
...
coroutine.resume(co)  -->co 10
coroutine.resume(co)  --> 什么都不打印,返回false

4)当协程A唤醒协程B时,协程A就处于normal状态。5)coroutine.resume-yield传参

function run_coroutine(thread)
    local status, value = coroutine.resume(thread)
    return value
end

function stop_coroutine(x)
    coroutine.yield(x)
end

function inputer()
    return coroutine.create(function()
        while true do
            local line = io.read()
            stop_coroutine(line)
        end
    end)
end

function outputer(thread)
    return coroutine.create(function()
        for line = ,  do
            local x = run_coroutine(thread)
            x = string.format("%5d Enter is %s", line, x)
            stop_coroutine(x)
        end
    end)
end

function main(thread)
    while true do
        local obtain = run_coroutine(thread)
        if obtain then
            io.write(obtain, "\n\n")
        else
            break
        end
    end
end

p = inputer()
f = outputer(p)
main(f)

11. Lua中的冒号,函数参数
a:add() == a.add(self, )  --即隐藏self参数。

--一个函数若只有一个参数,并且此参数是一个字符串或table构造式,那么函数调用的圆括号便可以省略掉。例如:
print "hello world" == print("hello world")
f{x=, y=}  ==  f({x=,y=})

12. Metatables
setmetatable(x, mt) -- use "mt" as the metatable for "x"
local x = {value = } -- creating local table x containing one key,value of value,5

local mt = {
  __add = function (lhs, rhs) -- "add" event handler
    return { value = lhs.value + rhs.value }
  end
}

setmetatable(x, mt) -- use "mt" as the metatable for "x"

local y = x + x

print(y.value) --> 10  -- Note: print(y) will just give us the table code i.e table: <some tablecode>

local z = y + y -- error, y doesn't have our metatable. this can be fixed by setting the metatable of the new object inside the metamethod

13. require 与 module

require用于搜索的Lua文件的路径存放在变量package.path中, 如果require无法找到与模块名相符的Lua文件,那Lua就会开始找C程序库;
当找到了这个文件以后,如果这个文件是一个Lua文件,它就通过loadfile来加载该文件;如果找到的是一个C程序库,就通过loadlib来加载。
loadfile和loadlib都只是加载了代码,并没有运行它们,为了运行代码,require会以模块名作为参数来调用这些代码

14. 变长参数...
    local moduleName = ...

    local arg = select(i, ...) -- 得到第i个参数
for i = , select('#', ...) do
print(select(i, ...))
end

调用select时,必须传入一个固定实参selector和一系列变长参数。
如果selector为数字n,那么select返回它的第n个可变实参以及其后面的所有参数
否则selector只能为字符串“#”,这样select会返回变长参数的总数

Userdata
. Thread
 
 
 
 

Lua学习笔记(1) ——语法的更多相关文章

  1. [转]LUA 学习笔记

    Lua 学习笔记 入门级 一.环境配置 方式一: 1.资源下载http://www.lua.org/download.html 2.用src中的源码创建了一个工程,注释调luac.c中main函数,生 ...

  2. Lua 学习笔记(一)

    Lua学习笔记 1.lua的优势 a.可扩张性     b.简单     c.高效率     d.和平台无关 2.注释 a.单行注释 --        b.多行注释 --[[  --]] 3.类型和 ...

  3. Lua学习笔记:面向对象

    Lua学习笔记:面向对象 https://blog.csdn.net/liutianshx2012/article/details/41921077 Lua 中只存在表(Table)这么唯一一种数据结 ...

  4. Lua学习笔记6:C++和Lua的相互调用

        曾经一直用C++写代码.话说近期刚换工作.项目组中的是cocos2dx-lua,各种被虐的非常慘啊有木有.     新建cocos2dx-lua项目.打开class能够发现,事实上就是C++项 ...

  5. Lua学习笔记4. coroutine协同程序和文件I/O、错误处理

    Lua学习笔记4. coroutine协同程序和文件I/O.错误处理 coroutine Lua 的协同程序coroutine和线程比较类似,有独立的堆栈.局部变量.独立的指针指令,同时又能共享全局变 ...

  6. (转)Lua学习笔记1:Windows7下使用VS2015搭建Lua开发环境

    Lua学习笔记1:Windows7下使用VS2015搭建Lua开发环境(一)注意:工程必须添加两个宏:“配置属性”/“C或C++”/“预处理器”/“预处理器定义”,添加两个宏:_CRT_SECURE_ ...

  7. Lua学习笔记(二):基本语法

    Lua学习指南:http://www.lua.org/manual/ 首先我们要明确的一点是:在Lua中,除了关键字外一切都是变量. Lua关键字 可以查看这个地址:http://www.lua.or ...

  8. Lua学习笔记一

    学习了有一周多了.之前一直不想献丑,但还是记录下这个过程. 第1章  开发软件搭建 1. ubuntu 下lua安装 sudo apt-get install lua5.1 2.win下的环境搭建. ...

  9. 热更新语言--lua学习笔记

    一.lua安装和编程环境搭建 lua语言可以在官网:http://luadist.org/下载安装包安装,编程IDE之前学习使用的是SciTE(https://www.cnblogs.com/movi ...

随机推荐

  1. AC日记——小行星 洛谷 P2711

    题目背景 pid=3437 题目描述 星云中有n颗行星,每颗行星的位置是(x,y,z).每次可以消除一个面(即x,y或z坐标相等)的行星,但是由于时间有限,求消除这些行星的最少次数. 输入输出格式 输 ...

  2. 洛谷—— P1875 佳佳的魔法药水

    https://www.luogu.org/problemnew/show/1875 题目背景 发完了 k 张照片,佳佳却得到了一个坏消息:他的 MM 得病了!佳佳和大家一样焦急 万分!治好 MM 的 ...

  3. logging模块详解以及常见代码

    1.在django中获取客户端IP地址: if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_ ...

  4. oceanbase 分布式数据库

    http://blog.csdn.net/maray/article/details/7230881 http://weibo.com/raywill2

  5. SSH错误:packet_write_wait: Connection to 10.57.19.250 port 22: Broken pipe

    现象:ssh连接以后,服务器会主动断开连接,wireshark抓包,发线服务器会tcp rst,断开ssh连接 解决尝试:1.修改会话超时时间:2.客户端主动间隔性向服务器发送保活报文:3.服务端主动 ...

  6. C# 使用xenocode混淆加密【转】

    http://www.cnblogs.com/chendaoyin/archive/2013/05/03/3056692.html   之前就了解过混淆加密工具,但这还是第一次使用,选择了xenoco ...

  7. poj-3744-Scout YYF I-矩阵乘法

    f[i]=f[i-1]*p+f[i-2]*(1-p); 正好能够用矩阵加速. . . . #include<stdio.h> #include<string.h> #inclu ...

  8. LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

    翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...

  9. require.js使用

    无可奈何,二开项目用了require.js! 一道槛是挨不过去了 require官网: http://requirejs.org/ require.js cdn: <script src=&qu ...

  10. 微信自带浏览器被输入法阻挡文本框的 jQuery 解决方法 by FungLeo

    微信自带浏览器被输入法阻挡文本框的 jQuery 解决方法 by FungLeo 前言 做好了项目之后,在各种浏览器里面測试,都没有问题.非常高兴,交付后端使用.然而发如今微信自带浏览器里面,却是出现 ...