I am trying to achieve a way that a new variable is being shifted to left, left to middle, and middle to right, the problem is I can’t quite wrap my head around this function and am therefore stuck with this current function and have no idea how to actually do what I am trying to achieve nor how to properly call it, any help is appreciated.
local Left,Middle,Right = "","",""
local function ShiftNewVarls()
local Next = ""
URLTable = TableModule.Shift(URLTable,DirectBool)
IDTable = TableModule.Shift(IDTable,DirectBool)
Next = {
Url = URLTable[1],
Id = IDTable[1]
}
if Left == "" or Left == nil then
Left = Next
elseif Middle == "" or Middle == nil then
Middle = Left
elseif Right == "" or Middle == nil then
Right = Middle
end
end
I believe a table-type approach to this would be a more intuitive approach
Whether you have left,middle,right or {1,2,3} it should not matter because you can convert the phrase left,middle,right to {left,middle,right}
I would imagine a table based approach would go something like this:
local lmr = {"a","b","c"}
function shift()
local temp = {} --create a temporary table so it doesnt interfere w/ itself
for i = 1,#lmr do --iterate through the whole tables indices
temp[i % #lmr + 1] = lmr[i]
--i % #lmr gets the remainder of the index and the number of items in lmr
--this allows the "remaining" (remainder) index (from when you move the last to first)
--to wrap around to the beginning again
--beyond this more confusing line it obviously just adds one to the index then assigns it
end
lmr = temp
--after all is said and done, you can finally give lmr temps value
--it cant interfere with itself anymore
end
shift()
print(lmr) --> c a b
If youre unsure how to convert Left,Middle,Right to a table, assuming your program requires a specific left middle and right variable for whatever reason, I would do something along the following lines:
local left,middle,right = "a","b","c"
function shift()
local lmr = {left,middle,right}
local temp = {}
for i = 1,#lmr do
temp[i % #lmr + 1] = lmr[i]
end
left,middle,right = unpack(temp)
end
shift()
print(lmr) --> c a b