【问题标题】:Lua shift element left and right in 2d arrayLua在二维数组中左右移动元素
【发布时间】:2014-05-06 21:11:23
【问题描述】:

我有一个二维数组:grid[x][y]

1     2     3     4     5     6     7     8
11    12    13    14    15    16    17    18
21    22    23    24    25    26    27    28
31    32    33    34    35    36    37    38
41    42    43    44    45    46    47    48

示例: 我想将第三列向下移动,底部元素进入第一行,如下所示:

41    2     3     4     5     6     7     8
 1    12    13    14    15    16    17    18
11    22    23    24    25    26    27    28
21    32    33    34    35    36    37    38
31    42    43    44    45    46    47    48

我使用以下函数将整个数组左右移动,但在二维数组中这显然不起作用,因为如果要移动元素,则必须将元素移动到另一个数组:

function wrap( t, l )
    -- change i=0 to move left and i=1 to right
    for i = 1, l do
        table.insert( t, 1, table.remove( t, #t ) )
    end
end

我在 Lua 暂存器中尝试了这个功能,但它不起作用......我无法在不丢失元素的情况下弄清楚逻辑。

function shift( t, direction )

for i=1,#t do
    if(direction == "left") then
            if(i == 1) then
                tempElement = t[#t][6] 
            else
                tempElement = t[i-1][6]
            end
    else
            if(i == 7) then
                tempElement = t[1][6] 
            else
                tempElement = t[i+1][6]
            end
    end    

table.insert( t[i], 6, tempElement )
table.remove( t[i], 12)

end

end

如何将元素转移到另一列但索引相同,所以grid[5][1] 进入grid[4][1] 等等。

【问题讨论】:

  • 请改写一下,清楚地说明您的表结构是什么,您要对其执行什么操作,以及您尝试过什么。
  • 您能否验证示例目标是您正在寻找的目标,我不确定。您还需要指出grid[x] 是代表您的示例的一行还是一列。
  • @Schollii codepad.org/Vrr5jGH6 我想通过列和行移动元素...
  • 对不起迈克尔,我看了看,但这并没有回答我的两个问题:1.你能检查一下你帖子中的第二张桌子代表你轮班后的桌子吗?如果没有,请修复帖子; 2. 是grid[1] 等于{1, 2, 3, ... 8}(行专业)还是{1, 11, 21, 31, 41}(列专业)?
  • @Schollii grid[1] 是 {1,2,3,...8}

标签: arrays lua shift lua-table


【解决方案1】:

根据您的 cmets,您正试图在 列中移动项目,例如:

{{11,12,13},
 {21,22,23},
 {31,32,33}}

到

{{31,12,13},
 {11,22,23},
 {21,32,33}}

以下代码使用了您的 wrap 函数:

g={{11,12,13},{21,22,23},{31,32,33}}

function shifItemWithinRow( array, shift )
    shift = shift or 1 -- make second arg optional, defaults to 1
    for i = 1, shift do
        table.insert( array, 1, table.remove( array, #array ) )
    end
end

function shifItemWithinColumn( grid, columnID, shiftCount )
    shiftCount = shiftCount or 1 -- make second arg optional, defaults to 1

    -- copy all items from g into new table, shifted:
    local numRows = #grid
    local newCol = {}
    for i=1,numRows do -- 
         local newI = i+shiftCount
         if newI > numRows then newI = newI - numRows end
         if newI < 1       then newI = numRows - newI end
         newCol[newI] = grid[i][columnID]
    end

    -- copy all items into grid
    for i=1,numRows do -- # each row
         grid[i][columnID] = newCol[i]
    end
end

function printGrid(g)
    for i, t in ipairs(g) do 
        print('{' .. table.concat(t, ',') .. '}')
    end
end

printGrid(g)
shifItemWithinColumn(g, 1) -- shift col 1 by +1
print()
printGrid(g)
print()
shifItemWithinColumn(g, 1, -1)  -- shift col 1 by -1
printGrid(g)

此示例将列移动 +1,然后移动 -1(因此 final 与 start 相同)。

【讨论】:

  • 它移动整个表格而不是元素?
  • 显示的解决方案是否移动整个表格?不,因为它只从第三行转移项目 (wrap(g[3]))。你想让它移动整个桌子吗?
  • 请看这个链接:codepad.org/lWv0BtrN in [1][1].key = 4, [2][1].key = 4,[3][1].key = 2 . 然后我用你的代码把它移到左边......它应该这样做 [1][1].key = 4, [2][1].key = 2,[3][1].key = 4。其余的保持不变......只是那些索引发生了变化
  • @MichaelAngelo 啊,所以你想转移列的项目!但这不是您帖子中的输出显示的内容(请看第二张表)。这就是为什么我要求您检查它并在必要时进行更正。请编辑您的帖子!我将编辑我的答案。
  • @Scholliii 这太完美了!这就是我一直想要的!我也可以用它来移动它吗(更新问题)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多