TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
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')
?
|
|
|
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.
|
|
TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
What would the structure look like in lua?
|
|
|
Probably how you did it, but more complicated due to the position argument.
|
|
KodranJoin Date: 2013-08-15 Post Count: 5330 |
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 |
|
TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
What do you mean position argument?
|
|
TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
oh. I didn't realize table.insert had a position argument. Welp time to refresh myself of table stuff. -_-
|
|
|
|
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.
|
|
TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
and thanks kodran
|
|
booingJoin Date: 2009-05-04 Post Count: 6594 |
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 |
|
TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
@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
|
|
KodranJoin Date: 2013-08-15 Post Count: 5330 |
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 |
|
KodranJoin Date: 2013-08-15 Post Count: 5330 |
and yeah it also would have to move everything but that would take too much effort. |
|
koolsJoin Date: 2009-01-11 Post Count: 1659 |
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. |
|
koolsJoin Date: 2009-01-11 Post Count: 1659 |
+1 for Kodran though using the and keyword like that. I only knew about the 'or'. |
|
TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
man that stuff is crazy
|
|
|
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 |
|
|
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 |
|
|
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 |
|
KodranJoin Date: 2013-08-15 Post Count: 5330 |
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 |
|
|
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 |
|