现在位置 >首页 > Lua
2℃
2012年08月24日 Lua ⁄ 被围观 14,116 次+
刚才写一个lua上实现php中的strpos()函数,下面在来个strrpos()函数,查找某个字符串在指定字符串最后一次出现的位置,下面我们还是简单写一下函数,代码如下: function strrpos (str, f)        if str ~= nil and f ~= nil then            local t = true           local offset = 1           local result = nil            while (t)            do               local tmp = string.find(str, f, offset)                if tmp ~= nil then                    offset = offset + 1                ...
阅读全文

Lua中实现php的strpos()函数 [原创]已关闭评论
2012年08月24日 Lua ⁄ 被围观 12,421 次+
在来写一个lua中实现php的strpos()函数,查找某个字符串在指定字符串首次出现的位置,其实lua中也为我们提供了这样的函数使用string.find()即可获得,下面我们还是简单写一个函数,代码如下: function strpos (str, f)        if str ~= nil and f ~= nil then            return (string.find(str, f))        else           return nil        end    end   测试如下图所示:
阅读全文
lua实现split字符串分隔 [原创]已关闭评论
2012年04月18日 Lua ⁄ 被围观 37,311 次+
LUA并不象其它许多"大而全"的语言那样,包括很多功能,比如网络通讯、图形界面等。但是LUA可以很容易地被扩展:由宿主语言(通常是C或C++)提供这些功能,LUA可以使用它们,就像是本来就内置的功能一样。LUA只包括一个精简的核心和最基本的库。这使得LUA体积小、启动速度快,从而适合嵌入在别的程序里。因此在lua中并没有其他语言那样多的系统函数。习惯了其他语言的字符串分割函数,与是就自己写了一个,记录在此,以备使用. 下面在简单介绍下lua: Lua 是一个小巧的脚本语言。作者是巴西人。该语言的设计目的是为了嵌...
阅读全文
3℃
2012年01月30日 Lua ⁄ 被围观 28,031 次+
lua 中pairs 和 ipairs的区别 ipairs (t) Returns three values: an iterator function, the table t, and 0, so that the construction for i,v in ipairs(t) do body end will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table. pairs (t) Returns three values: the next function, the table t, and nil, so that the construction for k,v in pairs(t) do body end will iterate over all key–value pairs of table t. See function next for the caveat...
阅读全文
3℃
2012年01月29日 Lua ⁄ 被围观 21,131 次+
之前写了个类似php的var_dump()函数,下面在来一个print_r()函数,代码如下: function pr (t, name, indent)        local tableList = {}        function table_r (t, name, indent, full)            local id = not full and name or type(name)~="number" and tostring(name) or '['..name..']'            local tag = indent .. id .. ' = '            local out = {}  -- result            if type(t) == "table" then                if tableList[t] ~= nil then                    table.insert...
阅读全文
lua实现php的var_dump()函数功能 [原创]已关闭评论
2012年01月20日 Lua ⁄ 被围观 14,244 次+
习惯了php中的var_dump()函数,而如今写lua的时候总习惯使用var_dump()函数,于是就自己动手写了一个类似功能的var_dump()函数。 function var_dump(data, max_level, prefix)        if type(prefix) ~= "string" then            prefix = ""       end        if type(data) ~= "table" then            print(prefix .. tostring(data))        else           print(data)            if max_level ~= 0 then                local prefix_next = prefix .. "    "               print(prefix .. "{")   ...
阅读全文
Lua5.1基本函数库介绍已关闭评论
2012年01月20日 Lua ⁄ 被围观 17,962 次+
Lua5.1基本函数库介绍 assert (v [, message]) 功能:相当于C的断言, 参数: v:当表达式v为nil或false将触发错误, message:发生错误时返回的信息,默认为"assertion failed!" collectgarbage (opt [, arg]) 功能:是垃圾收集器的通用接口,用于操作垃圾收集器 参数: opt:操作方法标志 "stop": 停止垃圾收集器 "restart": 重启垃圾收集器 "collect": 执行一次全垃圾收集循环 "count": 返回当前Lua中使用的内存量(以KB为单位) "step": 单步执行一个垃圾收集. 步长 "size" 由参数arg指定 (大型的值需要多步才能...
阅读全文
Lua5.1数学库函数参考已关闭评论
2012年01月20日 Lua ⁄ 被围观 16,847 次+
Lua5.1中数学库的所有函数如下表: math.pi 为圆周率常量 = 3.14159265358979323846 abs 取绝对值 math.abs(-15) 15 acos 反余弦函数 math.acos(0.5) 1.04719755 asin 反正弦函数 math.asin(0.5) 0.52359877 atan2 x / y的反正切值 math.atan2(90.0, 45.0) 1.10714871 atan 反正切函数 math.atan(0.5) 0.463647609 ceil 不小于x的最大整数 math.ceil(5.8) 6 cosh 双曲线余弦函数 math.cosh(0.5) 1.276259652 cos 余弦函数 math.cos(0.5) 0.87758256 deg 弧度转角度 math.deg(math.pi) 180 ex...
阅读全文
Lua5.1字符串函数库介绍已关闭评论
2012年01月20日 Lua ⁄ 被围观 13,959 次+
Lua解释器对字符串的支持很有限。一个程序可以创建字符串并连接字符串,但不能截取子串,检查字符串的大小,检测字符串的内容。在Lua中操纵字符串的功能基本来自于string库。 string库为Lua提供简易的字符串处理操作,所有的字串操作都是以1为基数的(C以0),也可使用负向索引,最后一个索引为-1 ; 所有的函数都存放在string表,并且已建立元表(__index=string表) 所以例如string.byte(s,i) <=> s:byte(i) string.byte(s [, i [, j]]) 功能:返回从i到j的字符所对应的数值(字符 到 ASCII值),i默认为1,j默认为i...
阅读全文
Wax 将Lua带人iPhone开发 [转]已关闭评论
2012年01月08日 Lua ⁄ 被围观 10,313 次+
【51CTO独家译文】2011年6月的编程语言排行榜Lua 语言一军突起,一举进入前十名。这与一年前苹果决定在iOS系统上使用Lua语言密不可分。但是,你了解如何用Lua语言在iOS上开发应用 吗?51CTO将向各位介绍Lua语言的iOS应用开发框架——Wax,其中在iOS平台上无比火爆的《愤怒的小鸟》就是由Lua语言用Wax开发的。全 文共分两部分,第一部分51CTO将带您深入探讨Wax具有的一些好处,同时演示把Lua与Xcode 4和iOS软件开发工具包(SDK)集成起来必不可少的实际步骤。第二部分51CTO将逐步介绍如何用Wax构建一个简单的应用程序...
阅读全文