Question

I have a 2d array : 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

Example: I would like to move the 3rd column down, the bottom element goes into the first row like this:

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

I used the following function to move the whole array left and right but in 2d array this obviously doesn't work, because you have to move the element to another array if shifting it:

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

I tried this function in Lua scratchpad and it doesn't work...I can't figure out the logic without losing the element.

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

How can I shift the elements to another column but same index, so grid[5][1] goes in grid[4][1] and so on.

Was it helpful?

Solution

According to your comments, you are trying to shift items within a column, example:

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

to

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

The following code uses your wrap function:

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)

This example shifts a column by +1 then by -1 (so final is same as start).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top