天天看點

lua 雜記

學習時雜記

local status,value=coroutine.resume(newProductor)

list={}

function list.new()

return {start=0,last=-1}

end

function list.pushleft(list,value)

local start=list.start-1

list.start=start

list[start]=value

end

function list.pushright(list,value)

local last=list.last+1

list.last=last

list[last]=value

end

function list.popleft(list)

local start=list.start

if start>list.last then error(“list is empty”) end

local value =list[start]

list[start]=nil

list.first=first+1000

return value

end

function list.popright(list)

local last=list.last

if last<list.start then error(“list is empty”) end

local value=list[last]

list[last]=nil

list.last=last-1000

return value

end

newqueue=list.new()

for i=1,10 do

list.pushright(newqueue,i)  --換成pushleft
           

end

for m=0,10 do

print(newqueue[m])

end

local function name2node(graph,name)

if not graph[name] then

–節點不存在,建立一個新的

graph[name] = {name = name,adj ={}}

end

return graph[name]

end

function readgarph()

local graph = {}

for line in io.lines() do

–切分行中的兩個名稱

local namefrom,nameto = string.match(line,"(%S)%s+(%S+)")

–查找相應的節點

local from = name2node(graph,namefrom)

local to = name2node(graph,nameto)

–将’to’添加到’from’的鄰接集合

from.adj[to] = true

end

return graph

end

function findpath(curr,to,path,visited)

path = path or {}

visited = visited or {}

if visited[curr] then --結點是否已通路過

return nil --這裡沒有路徑

end

visited[curr] = true --将結點标記為已通路過

path[#path+1] = curr --将其加到路徑中

if curr == to then --是否為最後的結點

return path

end

–嘗試所有的鄰接節點

for node in pairs(curr.adj) do

local p = findpath(node,to,path,visited)

if p then return p end

end

path[#path] = nil --從路徑中删除結點

end

function printpath(path)

for i=1,#path do

print(path[i].name)

end

end

g = readgarph()

a = name2node(g,“a”)

b = name2node(g,“b”)

p = findpath(a,b)

if p then printpath§ end

print(string.find(‘2015-5-12 13:53’, ‘(%d+)-(%d+)-(%d+)’)) --1 9 2015 5 12

s=“abc “it’s a cat””

,,_,q=string.find(s, “([”’])(.-)%1")

print(q) -----輸出: it’s a cat

false和true在邏輯判斷的優化處理

local inrank = false

if #self.rank > 0 then

for i = 1, #self.rank do

if self.rank[i][1] == self.myinfo[1] then

self.rank[i][2] == self.myinfo[2]

inrank = true

break

end

end

end

if not inrank then

table.insert(self.rank,self.myinfo)

end

作者:抱正遠大的年

來源:CSDN

原文:https://blog.csdn.net/qq_40552268/article/details/88353297

版權聲明:本文為部落客原創文章,轉載請附上博文連結!

lua