lua 文件读写

function split(a,b)
    count=0
    start = 1
    local arry = {}
    lastStart = string.find(a,b,start)
    if(lastStart~=nil)then
        count=count+1
    end
    table.insert(arry,1,string.sub(a,start,lastStart-1))
    while(lastStart~=nil)do--2,4,6
        start = string.find(a,b,lastStart+1)
        --print(start)--4,6,0
        if(start~=nil)then
            table.insert(arry,1,string.sub(a,lastStart+1,start-1))
            count=count+1
        else
            table.insert(arry,1,string.sub(a,lastStart+1,string.len(a)))
        end
        lastStart=start
    end
    return arry
end

function ReadLine(file)
    return file:read("*line")
end

function getTable()
    local file = io.open("lua\\1.lua","r")
    if(file==nil)then print("Error")end
    local arry={}
    local data=ReadLine(file)
    count = 0
    while data~=nil do
        count = count + 1
        --print(data)
        table.insert(arry,split(data,","))
        data = ReadLine(file)
    end
    --print(table.getn(arry))
    return arry
end

function getRandomCount()
    count = math.random(0,100)
    print(count)
    return count
end

function Lottery(item,arry)--抽奖
    for i=table.getn(arry),1,-1 do
        --print(tonumber(arry[i][1]),i)
        if(tonumber(arry[i][1])>=item)then
            return arry[i][2]
        end
    end
end

math.randomseed(os.time())
local arry = getTable()
local item = {}
for i=1,1000,1 do
    item = Lottery(getRandomCount(),arry)
    print(item)
end
无,100
倚天剑,80
屠龙刀,50
狼牙棒,10
擀面杖,1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值