2013-10-11 52 views
3

我无法弄清楚如何获得矩阵内矩阵的长度(嵌套深度为3)。因此,代码简而言之就是...查看发布者是否已经在数组中,然后它会在数组中添加一个新的列与新的发布者和相应的系统,或者将新系统添加到现有阵列出版商嵌套数组的长度lua

output[k][1]是出版商阵列 output[k][2][l]在系统

其中第一[]是不同的出版商 的量和第二个[]是相同的出版商内的不同系统的量

那么如何找出第三个深度数组的长度是多少?

function reviewPubCount() 
    local output = {} 
    local k = 0 
    for i = 1, #keys do 
     if string.find(tostring(keys[i]), '_') then 
      key = Split(tostring(keys[i]), '_') 
      for j = 1, #reviewer_code do 
       if key[1] == reviewer_code[j] and key[1] ~= '' then 
        k = k + 1 
        output[k] = {} 
        -- output[k] = reviewer_code[j] 
        for l = 1, k do 
         if output[l][1] == reviewer_code[j] then 
          ltable = output[l][2] 
          temp = table.getn(ltable) 
          output[l][2][temp+1] = key[2] 
         else 
          output[k][1] = reviewer_code[j] 
          output[k][2][1] = key[2] 
         end 
        end 
       end 
      end 
     end 
    end 
    return output 
end 

的代码已经被固定在这里以供将来参考:http://codepad.org/3di3BOD2#output

+0

哪里'ltable'和'temp'申报?因为它们现在出现在你的代码中,它似乎是全局变量。你忘了在他们面前或在你的设计中添加一个'local'关键字,他们真的是全局的? –

+0

@ user2872731第3级中哪一个表的长度? – dualed

+0

我真的会推荐使用描述性字符串而不是幻数。有一个名为'output [k] .publisher'和一个子表'output [k] .reviewer_codes'的字段可以让你的代码更加清洁,并且让你的生活更轻松。 – kikito

回答

3

你应该能够取代table.getn(t)#t(它不赞成在Lua 5.1和在Lua 5.2删除);而不是这样的:

ltable = output[l][2] 
temp = table.getn(ltable) 
output[l][2][temp+1] = key[2] 

试试这个:

output[l][2][#output[l][2]+1] = key[2] 

或本:

table.insert(output[l][2], key[2])