help with arrays (concatenate, indexof, splice)

i’ve posted some snippets from my collision, and have some questions around concat, indexof and splice. I’ve marked these up in the code. thanks for any help

( note: I use _.push for my array push from here http://mirven.github.com/underscore.lua )

[lua]-- ***** is this correct?
function indexof(t,e) return t[e] end
function splice(t,i) table.remove(t,i) end
– *****
– collision began (snippet)

obj1 = event.object1
obj2 = event.object2

if (obj1.actor == obj2.actor && obj1.actor.group[1] != obj1.actor.group[1]) then

obj1.actor.contacts.push(obj2.actor);
obj2.actor.contacts.push(obj1.actor);
– ***** need help doing this concatenate *******
– ***** what is the correct syntax for LUA? ****

obj1.actor.group = obj1.actor.group.concat(obj2.actor.group)

– *****

obj1.shareGroup()

– collision end (snippet)
obj1 = event.object1
obj2 = event.object2

if(obj1.actor.group.indexOf(obj2.actor) ~= nil) then

– ***** need help with the right syntax for splice (table.remove) & indexof *****
– ***** removes obj2 from obj1 contacts array, and obj1 from obj2’s

splice(obj1.actor.contacts,indexof(obj1.actor.contacts,obj2.actor))
splice(obj2.actor.contacts,indexof(obj2.actor.contacts,obj1.actor))

– *****

oldGroup = obj1.actor.group

for i=1, # oldGroup, 1 do

oldGroup[1].resetGroup()

end

obj1.actor.redefineGroup()
obj2.actor.redefineGroup()
obj1.actor.shareGroup()
obj2.actor.shareGroup()[/lua] [import]uid: 6645 topic_id: 3415 reply_id: 303415[/import]

that != should be ~=

taking me a while to move over from other languages!

also my condition should be

[lua]if (obj1.actor == obj2.actor and obj1.actor.group[1] ~= obj2.actor.group[1]) then[/lua]
[import]uid: 6645 topic_id: 3415 reply_id: 10258[/import]

i found these separately

[lua]function concat(t1, t2) for k,v in ipairs(t2) do table.insert(t1, v) end return t1 end

function splice(t,i) table.remove(t,i) end

function indexof(t,obj)

for k,v in pairs(t) do
if(v==obj) then return k end
end

end[/lua]

testing them at http://www.lua.org/cgi-bin/demo all seems to work fine

[lua]obj1 = {id = “iz obj1”}
obj2 = {id = “iz obj2”}
obj3 = {id = “iz obj3”}
obj4 = {id = “iz obj4”}

print("obj1.id "…obj1.id)

group1 = {obj1,obj2}
group2 = {obj3,obj4}

print(“concat group1 and group2”)
concat(group1,group2)
print(“group1[3]=”…group1[3].id)

print(“splice(group1,1) – removes obj1”)
splice(group1,1)
print(“group1[1]=”…group1[1].id)

temp = indexof(group1, obj4)

if(temp ~= nil) then print(“indexof(group1,obj4)=”…temp)
else print(“is nil”)
end[/lua]
[import]uid: 6645 topic_id: 3415 reply_id: 10262[/import]

A more traditional splice, where you can chop out sections and replace with more loot.

NOTE: Not fully hashed out, can only “replaceWith” one “thing” (can’t insert another array)

[lua]function splice(t,i,len, replaceWith)
if (len > 0) then
for r=0, len do
if(r < len) then
table.remove(t,i + r)
end
end
end
if(replaceWith) then
table.insert(t,i,replaceWith)
end
local count = 1
local tempT = {}
for i=1, #t do
if t[i] then
tempT[count] = t[i]
count = count + 1
end
end
t = tempT
end[/lua]

Usage:
[lua]myArray = {1,2,3,4,5,6,7}

splice(myArray, 3, 1, “bob”)
– sets myArray to:
– 1,2,bob,4,5,6,7

splice(myArray, 3, 3, “bob”)
– sets myArray to:
– 1,2,bob,6,7[/lua] [import]uid: 616 topic_id: 3415 reply_id: 23401[/import]