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))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值