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).