记录下第一次用lua

时间:2021-04-11 16:53:36

因为项目需要,最近开始接触lua,需求倒是很简单,策划给出一张csv表,第一列是属性,第二列是概率,让找出四个不同的属性。


使用链表的做法


function GetLines(fileName)
indx = 0
myLines = {}
for line in io.lines(string.format("%s%s", "./", fileName)) do
indx = indx + 1
myLines[indx] = line
end
return indx, myLines
end



function GetValues(myString)
local num = 0
local values = {}
if myString ~= nil then
while string.find(myString,",") ~= nil do
i,j = string.find(myString,",")
num = num + 1
values[num] = string.sub(myString,1, j-1)
myString = string.sub(myString, j+1, string.len(myString))
end
num = num + 1
values[num] = myString
end
return num, values
end


function ReadCsv(fileName, list)

local all_num = 0
local index = 1

zt_v = {["name"]="HEAD", ["data"]=tonumber(0)}
list = {next=list, value=zt_v}

local _, myLines = GetLines(fileName)
for k,v in ipairs(myLines) do
if k>1 then
local _, values = GetValues(v)
local n_v2 = tonumber(values[2]) * 100

t_v = {["name"]=values[1], ["data"]=n_v2}
list ={next=list, value=t_v}

all_num = all_num + n_v2
end
end

return all_num, list
end

function p_table(tb)
for _,k in pairs(tb) do
print(_, k)
end
end

function p_list(list)
local l = list

while l do
if l ~= nil and l.next ~= nil then
print(l.value["name"], l.value["data"])
end

l = l.next
end

end


function GetValue(num, list)
local addlen = 0
local l = list

while l do
if l ~= nil and l.next ~= nil and l.next.next ~= nil then

local right = addlen + l.next.value["data"]

if num > addlen and num <= right then
local getName, getValue = l.next.value["name"], l.next.value["data"]

l.next = l.next.next;

return getName, getValue
end

addlen = right
end


l = l.next

end
end

---------------------------------------------------------------------


local list = {}
local all_num, list = ReadCsv("data.csv", list)

print("-------------------")
print(all_num)
p_list(list)
print("-------------------")

math.randomseed(tostring(os.time()):reverse():sub(1, 6))
for l=1,4,1 do
local rand = math.random(all_num)
local getName, getValue = GetValue(rand, list)
all_num = all_num - getValue

print("---------------")
--print(all_num)
print("["..getName..", "..getValue.."]")
print("---------------")
p_list(list)

end


简单点,直接使用table


function GetLines(fileName)
indx = 0
myLines = {}
for line in io.lines(string.format("%s%s", "./", fileName)) do
indx = indx + 1
myLines[indx] = line
end
return indx, myLines
end



function GetValues(myString)
local num = 0
local values = {}
if myString ~= nil then
while string.find(myString,",") ~= nil do
i,j = string.find(myString,",")
num = num + 1
values[num] = string.sub(myString,1, j-1)
myString = string.sub(myString, j+1, string.len(myString))
end
num = num + 1
values[num] = myString
end
return num, values
end


function ReadCsv(fileName)

local all_num = 0
local index = 1

local v_table = {}

local _, myLines = GetLines(fileName)
for k,v in ipairs(myLines) do
if k>1 then
local _, values = GetValues(v)
local n_v2 = tonumber(values[2]) * 100

v_table[values[1]] = n_v2

all_num = all_num + n_v2
end
end

return all_num, v_table
end

function p_table(tb)
for _,k in pairs(tb) do
print(_, k)
end
end

function p_list(list)
local l = list

while l do
if l ~= nil and l.next ~= nil then
print(l.value["name"], l.value["data"])
end

l = l.next
end

end

function GetValue(num, v_table)
local left = 0
local right = left

local i = 1
for k,v in pairs(v_table) do
right = right + v

if num > left and num <= right then
local gName = k
local gValue = v_table[k]

v_table[k] = nil
return gName, gValue
end

left = right
i = i + 1
end
end


---------------------------------------------------------------------


local all_num, v_table = ReadCsv("data.csv", list)

print("-------------------")
print(all_num)
print("-------------------")
p_table(v_table)



math.randomseed(tostring(os.time()):reverse():sub(1, 6))
for l=1,4,1 do
local rand = math.random(all_num)
local gName, gValue= GetValue(rand, v_table)

print("--------------")
p_table(v_table)
print("--------------")

all_num = all_num - gValue

print("---------------")
--print(all_num)
print("["..gName..", "..gValue.."]")
print("---------------")


end


使用正则处理字符串的


--[[
正则处理,输入指定格式字符串,返回处理后的table
]]

function handleString(inputString)
v_table = {}

local str = inputString
local header = string.match(str, "(%a+)|")
table.insert(v_table, header)

str = string.gsub(str, header.."|", "")
local iteratorFunc = string.gmatch(str, "%a+=[^,^|]*")
for var in iteratorFunc do
local key, value = string.match(var, "(%a+)=([^,^|]*)")
if value == nil then
value = ""
end

v_table[key] = value
end
return v_table;
end


----------------------------------------------------

local inputString = "lgn|userid=50151910,tm=1476326691,sign=d6a6b92a41c39d86985057123611ee7b,ver=0.0.0.5.4842,ch=9876567,account=,svrid=qmsj0001,deviceid=,channeluser=0,sdkver=,devicemodel=,oriuid=1775564699352569,vip_xinyue=-1"

g_table = handleString(inputString)

for k,v in pairs(g_table) do
print(k, v)
end