`

Lua 程序集锦

    博客分类:
  • lua
lua 
阅读更多

String

 

print(table.concat({"a","b","c"},","))
a,b,c

 

 

function string:split(sep)
	local sep,fields  = sep or "," , {}
	local pattern = string.format("([^%s]+)",sep)
	self:gsub(pattern,function(c)
		fields[#fields+1] = c
	end)
	return fields
end

a = "1,2,3"
splits = a:split(",")

for _,i in ipairs(splits) do
	print(i)
end

 

File

 

function length_of_file(filename)
  local fh = assert(io.open(filename, "rb"))
  local len = assert(fh:seek("end"))
  fh:close()
  return len
end

-- Return true if file exists and is readable.
function file_exists(path)
  local file = io.open(path, "rb")
  if file then file:close() end
  return file ~= nil
end


print(length_of_file("/Users/liuzheng/lua/fact.lua"))
print(file_exists("/Users/liuzheng/lua/"))
 

携程

 

require "luasocket"

-- warning: depending on the version of luasocket that we use,
-- we have to change `settimeout' to `timeout' (as done here)


function receive (connection)
  connection:timeout(0)   -- do not block
  local s, status = connection:receive(2^10)
  if status == "timeout" then
    coroutine.yield(connection)
  end
  return s, status
end


function download (host, file)
  local c = assert(socket.connect(host, 80))
  local count = 0    -- counts number of bytes read
  c:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
  while true do
    local s, status = receive(c)
    count = count + string.len(s)
    if status == "closed" then break end
  end
  c:close()
  print(file, count)
end


threads = {}    -- list of all live threads
function get (host, file)
  -- create coroutine
  local co = coroutine.create(function ()
    download(host, file)
  end)
  -- insert it in the list
  table.insert(threads, co)
end


function dispatcher ()
  while true do
    local n = table.getn(threads)
    if n == 0 then break end   -- no more threads to run
    local connections = {}
    for i=1,n do
      local status, res = coroutine.resume(threads[i])
      if not res then    -- thread finished its task?
        table.remove(threads, i)
        break
      else    -- timeout
        table.insert(connections, res)
      end
    end
    if table.getn(connections) == n then
      socket.select(connections)
    end
  end
end


host = "www.w3.org"

get(host, "/TR/html401/html40.txt")
get(host,"/TR/2002/REC-xhtml1-20020801/xhtml1.pdf")
get(host,"/TR/REC-html32.html")
get(host, "/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt")

dispatcher()   -- main loop
 

 

二分查找

 

function table.binsearch(t,v) 
   local start = 1
   local ends = #t
   local mid 

   while start <= ends do
   		mid = (ends + start) / 2
   		if t[mid] == v then
   			return mid
   		elseif t[mid] > v then
   			ends = mid - 1
   		else
   			start = mid + 1
   		end
   end
end

t = {1,2,3}
print(table.binsearch(t,2))
print(table.binsearch(t,3))
print(table.binsearch(t,4))
1
0
分享到:
评论

相关推荐

    LUA程序设计教程LUA程序设计教程LUA程序设计教程

    LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程LUA程序设计教程

    LUA 程序设计

    LUA 程序设计 LUA 程序设计 LUA 程序设计 LUA 程序设计

    Lua程序设计(第四版)

    Lua程序设计第四版,带书签,英文高清, MOBI PDF两个版本

    lua程序设计.pdf

    lua开发文档,当今武林,派别林立,语言繁杂,林林总总不计其数。主流文化的C/C++、Java、 C#、VB;偏安一隅的Fortran;动态语言中的Perl、Tcl、Ruby...可欣赏一下BrainFuck语言1的Hello World程序,语言本身依如其名

    lua程序设计中文版

    lua程序设计中文lua程序设计中文lua程序设计中文版

    lua程序设计及lua中文手册

    lua程序设计,lua中文手册,lua相关资料

    lua程序设计电子书

    lua程序设计电子书 lua入门经典书籍

    Lua程序设计 (Programming in Lua) [电子书集合].rar

    Lua程序设计 (Programming in Lua) [电子书集合].rar ================================== 压缩包中包函如下内容: Programming_in_Lua_First.txt (官方在线第一版地址) Programming_in_Lua_Second.pdf (官方英文...

    Lua程序设计和lua-5.1中文手册

    lua-5.1中文手册.chm Lua程序设计.chm

    Lua程序设计&5.1中文手册&5.1源码

    Lua程序设计 5.1中文手册 5.1源码 Lua程序设计 5.1中文手册 5.1源码 Lua程序设计 5.1中文手册 5.1源码

    LUA程序设计一书的源码

    《Lua程序设计》(第2版)介绍了Lua语言所具有的功能,并使用大量示例来演示如何将它们运用到实际的任务中。深入地介绍了Lua中唯一的数据结构 table,还讨论了数据结构、持久化、包和面向对象编程。展示了Lua的标准库...

    Lua程序设计_书籍

    《Lua程序设计》是(巴西)莱鲁萨利姆斯奇编著的一本图书,该书介绍了Lua语言所具有的功能,并使用大量示例来演示如何将它们运用到实际的任务中。深入地介绍了Lua中唯一的数据结构table,还讨论了数据结构、持久化、...

    Lua程序与设计第四版 英文原版

    Lua程序与设计第四版 英文原版 epub格式 可以用浏览器打开 带书签目录

    编写Lua程序并检查语法的工具

    BluaAlphaSetup.exe 是专门编写Lua程序并检查语法的工具,推荐使用此工具来书写Lua程序,查看“代码”目录下的源文件,也推荐使用此工具。 解压后 Ultra Edit32 是编程通用工具,也可以用此工具来书写Lua程序,以及...

    Lua 程序设计

    作为中文版的《Programming In Lua》,本书遵循其第二版的书名规则,命名为《Lua 程序设计》。 本书主要针对的 Lua 版本号为 5.0,到目前为止尚无出版信息可查。正在发行中的《Lua 程序设计》第 二版主要针对 Lua ...

    《lua程序设计》chm

    《lua程序设计》 在线查看:http://book.luaer.cn 由http://www.luaer.cn 中国lua开发者友情提供!

    lua程序设计和参考手册

    如何让EditPlus支持LUA; lua-5.1.4.tar.gz; lua程序设计中文版.pdf; Lua+5.1+参考手册.pdf; Lua一些基本函数.txt; Programming_in_LUA中文版.txt; Lua 5_1 参考手册.mht;

    Lua程序设计.pdf_lua_nginx_

    lua程序设计包含了lua的基础和进阶方面的只是,循序渐进,适合新手学习

    Lua程序设计_高清

    Lua程序设计,很适合想学Lua的同学学习,同时适合搞Unity3D或者redis的同学学习

Global site tag (gtag.js) - Google Analytics