Code: Select all
-- table.rotate(t,value) value accepting positive and negative numbers
table = {1, 2 ,3 ,4 ,5}
table.rotate(table,1) -- {2, 3, 4, 5, 1}
table2 = {1, 2 ,3 ,4 ,5}
table.rotate(table2,-1) -- {5, 1, 2, 3, 4}
Code: Select all
-- table.rotate(t,value) value accepting positive and negative numbers
table = {1, 2 ,3 ,4 ,5}
table.rotate(table,1) -- {2, 3, 4, 5, 1}
table2 = {1, 2 ,3 ,4 ,5}
table.rotate(table2,-1) -- {5, 1, 2, 3, 4}
Code: Select all
-- left right by 1
t = {1, 2 ,3 ,4 ,5}
local last = table.remove(t)
table.insert(t, 1, last) -- {5, 1, 2, 3, 4 }
Code: Select all
t = {1, 2 ,3 ,4 ,5}
function get_value_by_shift(t, index, shift)
index = (index + shift - 1)%#t + 1
return t[index]
end
Code: Select all
function table.rotate ( t, num )
for i = 1, math.abs ( num ) do
if num < 0 then
table.insert ( t, 1, table.remove ( t ) )
else
table.insert ( t, table.remove ( t, 1 ) )
end
end
end
Code: Select all
local function rotate (t, n)
local len, out = #t, {}
for i = 1, len do out[i] = t[1 + (i - 1 + n) % len] end
return out
end
Users browsing this forum: Google [Bot] and 3 guests