TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 22 Jul 2016 01:32 PM |
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')
?
|
|
|
| Report Abuse |
|
|
|
| 22 Jul 2016 01:35 PM |
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.
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 22 Jul 2016 01:36 PM |
What would the structure look like in lua?
|
|
|
| Report Abuse |
|
|
|
| 22 Jul 2016 01:38 PM |
Probably how you did it, but more complicated due to the position argument.
|
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 22 Jul 2016 01:41 PM |
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 |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 22 Jul 2016 01:42 PM |
What do you mean position argument?
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 22 Jul 2016 01:43 PM |
oh. I didn't realize table.insert had a position argument. Welp time to refresh myself of table stuff. -_-
|
|
|
| Report Abuse |
|
|
|
| 22 Jul 2016 01:43 PM |
The position argument is this: table.insert(table, "value", 2)
( ͡• ◡ ͡•) -=[ RAP: 368,330 || DurstAuric; the narb of ROBLOX ]=- ( ͡• ◡ ͡•) |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2016 01:44 PM |
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.
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
| |
|
booing
|
  |
| Joined: 04 May 2009 |
| Total Posts: 6594 |
|
|
| 22 Jul 2016 01:45 PM |
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 |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 22 Jul 2016 01:46 PM |
@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
|
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 22 Jul 2016 01:49 PM |
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 |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 22 Jul 2016 01:50 PM |
| and yeah it also would have to move everything but that would take too much effort. |
|
|
| Report Abuse |
|
|
kools
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 1659 |
|
|
| 22 Jul 2016 02:26 PM |
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. |
|
|
| Report Abuse |
|
|
kools
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 1659 |
|
|
| 22 Jul 2016 02:28 PM |
| +1 for Kodran though using the and keyword like that. I only knew about the 'or'. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
| |
|
|
| 22 Jul 2016 02:49 PM |
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 |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2016 02:49 PM |
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 |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2016 02:50 PM |
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 |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 22 Jul 2016 03:33 PM |
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 |
|
|
| Report Abuse |
|
|
|
| 22 Jul 2016 03:56 PM |
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 |
|
|
| Report Abuse |
|
|