of     1   

TimeTicks
#194292235Friday, July 22, 2016 6:32 PM GMT

is this the basic way it works? local tab = {} function tab:insert(self,object) return self[#self+1] = object end --- local hi = {} tab.insert(hi,'hello') hi:insert('hello') ?
JarodOfOrbiter
#194292421Friday, July 22, 2016 6:35 PM GMT

Nope, you forgot the position argument. The OOP method syntax is unnecessary, but I won't mark that against you since the real function is probably in C and so it is neither.
TimeTicks
#194292505Friday, July 22, 2016 6:36 PM GMT

What would the structure look like in lua?
JarodOfOrbiter
#194292669Friday, July 22, 2016 6:38 PM GMT

Probably how you did it, but more complicated due to the position argument.
Kodran
#194292911Friday, July 22, 2016 6:41 PM GMT

local tab = {} function tab.insert(t, obj, index) t[index or #t+1] = obj end would work, or slightly more accurately: local tab = {} function tab.insert(t, index, obj) if obj then t[index] = obj else t[#t+1] = index end end cause it does weird stuff where this: table.insert(table, "hi") inserts it at the end but table.insert(table, 5, "hi") inserts it in the fifth slot
TimeTicks
#194292917Friday, July 22, 2016 6:42 PM GMT

What do you mean position argument?
TimeTicks
#194292987Friday, July 22, 2016 6:43 PM GMT

oh. I didn't realize table.insert had a position argument. Welp time to refresh myself of table stuff. -_-
[rfa#hidefromsearch]
#194293030Friday, July 22, 2016 6:43 PM GMT

[rfa#hidefromsearch]
JarodOfOrbiter
#194293087Friday, July 22, 2016 6:44 PM GMT

Even that is wrong, Kodran. table.insert slides everything past the insert point to the right. And I just realized, TimeTicks, that the very last line of code you gave wouldn't work. Again, the OOP stuff is kind of unnecessary.
TimeTicks
#194293136Friday, July 22, 2016 6:45 PM GMT

and thanks kodran
booing
#194293156Friday, July 22, 2016 6:45 PM GMT

It makes no sense to try to understand something with inferences when there's well-commented source out there https://www.lua.org/source/5.1/ltablib.c.html#tinsert
TimeTicks
#194293250Friday, July 22, 2016 6:46 PM GMT

@Jarod yeah i realized that after testing it. I thought it would work at first because you have string.lower where you can do local hi = 'hi' if hi:lower() == 'hi' then
Kodran
#194293442Friday, July 22, 2016 6:49 PM GMT

Here i wrote the second way better local tab = {} function tab.insert(t, index, obj) t[(index and obj) or #t+1]= (obj and index) or index end
Kodran
#194293508Friday, July 22, 2016 6:50 PM GMT

and yeah it also would have to move everything but that would take too much effort.
kools
#194296245Friday, July 22, 2016 7:26 PM GMT

Booing made a good point. Here is the table insert code "ported" from C. I included the comments too. static int tinsert (...) { args = {...} e = #table + 1 -- first empty element local pos -- where to insert new element if #args == 2 then -- called with only 2 arguments pos = e -- insert new element at the end else if #args == 3 then local i assert(type(args[2]) == "number", "Invalid input: arg2 is not a number!") -- integer types were only added recently I couldn't port this too well. assert(args[2] % 1 == 0, "Invalid input: arg2 is not an integer") pos = args[2] -- 2nd argument is the position if (pos > e) e = pos -- `grow' array if necessary for i = e, pos, -1 do -- move up elements args[1][i] = args[1][i-1] -- t[i] = t[i-1] end end else error("wrong number of arguments to table insert") end args[1][pos] = arg[3] or arg[2] -- t[pos] = v end Or something like that. I haven't tested it. It may or may not work.
kools
#194296389Friday, July 22, 2016 7:28 PM GMT

+1 for Kodran though using the and keyword like that. I only knew about the 'or'.
TimeTicks
#194296475Friday, July 22, 2016 7:29 PM GMT

man that stuff is crazy
warspyking
#194297955Friday, July 22, 2016 7:49 PM GMT

local insert(tbl, pos, val) if not val then tbl[#tbl+1] = post return end for i = #tbl,pos do tbl[i+1] = tbl[i] end tbl[pos] = val return end
warspyking
#194297993Friday, July 22, 2016 7:49 PM GMT

lol'ing at my syntax error forgetting to define a function local function insert(tbl, pos, val) if not val then tbl[#tbl+1] = post return end for i = #tbl,pos do tbl[i+1] = tbl[i] end tbl[pos] = val return end
warspyking
#194298038Friday, July 22, 2016 7:50 PM GMT

Gosh dangit why'd I wrote post.. local function insert(tbl, pos, val) if not val then tbl[#tbl+1] = pos return end for i = #tbl,pos do tbl[i+1] = tbl[i] end tbl[pos] = val return end If there's more mistakes I refuse to fix them rofl
Kodran
#194301308Friday, July 22, 2016 8:33 PM GMT

Here's a function that 100% works and shifts everything correctly: local tab = {} function tab.insert(t, index, obj) for i = #t+1, ((obj and index) or #t+1), -1 do t[i+1]=t[i] end t[(obj and index) or #t+1] = (index and obj) or index end
warspyking
#194303138Friday, July 22, 2016 8:56 PM GMT

I know I said I refuse to fix but.. local function insert(tbl, pos, val) if not val then tbl[#tbl+1] = pos return end for i = #tbl,pos, -1 do --I forgot the -1 lol tbl[i+1] = tbl[i] end tbl[pos] = val return end

    of     1