如何在Lua中实现条件循环和随机选择

时间:2017-08-21 14:34:55

标签: loops variables random lua conditional-statements

我想获取一个随机记录(使用两个API),它有一个图像并显示给用户。此外,由于获取的记录包含不同的值,我想稍后在不同的Lua文件中重复使用它们。

在下面的代码中,value.original.source是图像URL(在第二个API调用中)并且在记录中可以为空(nil),因此我认为我需要继续获取记录,而它是空的(使用repeat-untilwhile-do?)。也许我需要使用全局变量将数据传递给不同的文件,但不确定。这很棘手且具有挑战性。任何想法和解决方案(部分或全部)都受到高度赞赏。谢谢!

--Create empty tables
t = {}
t2 = {}

-- 1st API call
local json = require( "json" )
local function getWikipediaData( event )
    local res = json.prettify( event.response )
    local decoded = json.decode( res )  
    for key, value in pairs(decoded.results.bindings) do 
        local pname = value.person_name.value
        local dbpuri = value.person.value
        -- Put data into a table 
        t["PNAME"] = pname
        t["DBPURI"] = dbpuri
        -- Print key-value
        for key,value in pairs(t) do 
            print(key,value) 
        end
        local wikititle = string.gsub(dbpuri, "http://dbpedia.org/resource/", "")
        print (wikititle)

        -- 2nd API call in which a function is nested
        network.request(
        "https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles="..wikititle,"GET",
        function(event) 
            local res2 = json.prettify( event.response )
            local decoded2 = json.decode( res2 )
            -- This part is the tricky part I have problems. "value.original.source" can be empty (nil) in a record, so I would like to keep fetching records, while it is empty. Then, among the fetched records (with value.original.source in them), finally I would like to select one record randomly. The data in the record (in a variable) should be passed to other files.
            while (pickdata == nil) do
                -- Next level to "pages" is array, so for is used to repeat fetching data
                for key, value in pairs(decoded2.query.pages) do 
                    -- Put data into a table
                    local source = value.original.source
                    table.insert(t2, source)
                end
            end
        end,params)
    end
end

-- This is the second task to try to randomnly select a record with an image from the table created, but does not work. Looping and local vs global variables are the issues to be resolved.
local pick = math.random(3)
local pickdata = t2[pick]
print (pickdata)

local headers = {}
headers["Content-Type"] = "application/json"
local body = ""
local params = {}
params.headers = headers
params.body = body
network.request("https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=select+*%0D%0Awhere+{%3Fperson+rdfs%3Alabel+%3Fperson_name+%3B+rdf%3Atype+%3Ftype+%3B+dbo%3AbirthDate+%3Fbirthdate+.%0D%0A++++bind(rand(1+%2B+strlen(str(%3Fperson))*0)+as+%3Frid)%0D%0AFILTER+regex(%3Ftype%2C+%22Person%22)%0D%0A}+order+by+%3Frid%0D%0ALIMIT+10&format=text%2Fhtml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on&format=json", "GET", networkListener, params)

0 个答案:

没有答案