2013-04-26 43 views

回答

2
local function copyNOtherElements(table, interval, startpos) 

local elemno = 1 
local rettab = {} 

for k, v in ipairs(table) do 
    if k >= startpos and (k - startpos) % interval == 0 then 
     rettab[elemno] = v 
     elemno = elemno + 1 
    end 
end 

return rettab 

end 

對不起,格式化,打電話。這是假設該表是基於1的陣列

+0

非常感謝,看着將它插入Redis調用中,並在我遇到任何Redis特定問題時向我們報告。看起來不錯(表格確實是一個基於1的數組)。 – lollipierre 2013-04-26 14:23:39

+0

@lollipierre將rettab更改爲本地。我的錯誤 – 2013-04-26 14:33:31

+0

是的,我發現它謝謝 – lollipierre 2013-04-26 15:08:06

2

對於未來的讀者,加入到Redis的以前的答案,有點更高效的代碼以迭代的第N個元素:

local function zrange_pick(zset_key, step, start, stop) 
    -- The next four lines can be removed along with the start/stop params if not needed as in OP Q. 
    if start == nil than 
     start = 0 
    if end == nil than 
     end = -1 

    local set_by_score = redis.call('ZRANGE', zset_key, start, end) 
    local result = {} 
    for n = 1, #set_by_score, step do 
     table.insert(result, set_by_score[n]) 
    end 
    return result 
end 
相關問題