Jump to content

table.remove


IIYAMA

Recommended Posts

  • Moderators

I have a problem with removing data from a table.

When I use table.remove the table will automatic change.

Sample:

local A = createObject(...,...,...,..)

local B = createObject(...,...,...,..)

local myTable = {A,2,8,B}

for i, v in ipairs (myTable) do

if not isElement( vehicle ) then

table.remove(myTable,i)

end

end

When I do this:

-- {A,2,8,B} --

First loop A won't be removed because it is an element.

Next 2 is a number so will be removed. (i=2)*

8 will be at location [2] because of table.remove(myTable,i=2)*.

But 8 since is there.... he won't remove

Index during table.remove

What is the best way to solve this?

Link to comment

That's an exciting problem, but I think I solved it now.

I know what was wrong in the first one. But now I had an another idea:

    local temp = {} 
    for i, v in ipairs (myTable) do 
        if not isElement( myTable[i]) then 
            table.insert( temp, i ) 
        end 
    end 
    for i, v in ipairs(temp) do 
        table.remove( myTable, v-(i-1)) 
    end 
    temp = {} 

Won't run for ever, and will remove every value from the table, wich fits the condition.

Maybe there is some easier solution, I'll be thinking about it.

Link to comment
for i, v in ipairs ( myTable ) do 
    while not isElement( myTable[ i ] ) do 
        table.remove( myTable, i ) 
    end 
end 

In his example, indexes are numbers, so this will remove everything from the array. Also, "myTable[ i ]" is not needed inside an iteration statement.

Link to comment

Indexes are always number, aren't they? And it wont remove everything, just in some cases it doesn't stop.

But I fixed it:

    for i, v in ipairs ( myTable ) do 
        while myTable[ i ] and not isElement( t[ myTable ] ) do 
            table.remove( myTable, i ) 
        end 
    end 

Edit: I know an index can be anything (e.g. table["something"] = 1 ). But in this case i would not call it index. Or do you still call it so?

Link to comment
  • Moderators
Indexes are always number, aren't they? And it wont remove everything, just in some cases it doesn't stop.

But I fixed it:

    for i, v in ipairs ( myTable ) do 
        while myTable[ i ] and not isElement( t[ myTable ] ) do 
            table.remove( myTable, i ) 
        end 
    end 

  
  
for i, v in ipairs ( myTable ) do 
        while myTable[ i ] and not isElement( v[i ] ) do 
            table.remove( myTable, i ) 
        end 
    end 

btw my element I need to check is here: v[1]

v[2] = weapon1

v[3] = weapon2

element, weapon1, weapon2 = unpack(v)

like this?

Note: while is dangerous stuff, even my intel boxed can crash of it....

Edited by Guest
Link to comment

Indexes aren't always numbers, they can be anything else (but by default, unless you add them by yourself, it will be numbers). And yes, in your example it will remove everything since no index is an element.

On your last code, I didn't get some things:

while myTable[ i ] 

I already said you don't need that in iteration statements.

not isElement( t[ myTable ] ) do 

Okay.. What is that?

@IIYAMA, it's still not right. 'v' is the value, not an array. You can't do "v", it doesn't even make sense.

Link to comment
not isElement( t[ myTable ] ) do 

This really doesn't makes sense, just forget to change every variable cause i tested it with something else, it's fixed now .

while myTable[ i ] 

Just try it with v instead of myTable[ i ] :) . After it removed one value, v won't be equal to myTable[ i ].

Link to comment
  • Moderators

aha got it, thank you

server

    for i,vehicle in ipairs (weaponObjectsVeh) do 
        while weaponObjectsVeh[ i ] and not isElement(weaponObjectsVeh[ i ]) do 
            table.remove(weaponObjectsVeh,i) 
        end 
    end 

client

        for i, v in ipairs (weaponObjectsVeh) do  
            --if not isElement(v[1]) then 
            while weaponObjectsVeh[ i ] and not isElement( (weaponObjectsVeh[i])[1] ) do 
                local vehicle2,weapon1,weapon2 = unpack(weaponObjectsVeh[i]) 
                destroyElement (weapon1) 
                if isElement(weapon2) then 
                    destroyElement (weapon2) 
                end 
                table.remove(weaponObjectsVeh,i) 
            end 
        end 

can it be smaller? because I call this index like 4x.

Link to comment
  • Moderators

(weaponObjectsVeh)[1]

= weaponObjectsVeh

theVehicle = v[1]

attached to the vehicle.

weapon1=v[2]

weapon2=v[3]

I can't check a "table" if it is an element.

When the vehicle get removed(gone) this filter will remove the weapons from the vehicles. The server table and the client together are working with the same data. Why I work with tables instead of element data is that I can decide when I send data to those players. If a player is in the spawn menu, he should start downloading new data at moments he really joins other players.

Also when they are doing a minigame, it isn't necessary to download those data.

Thank you again.

Link to comment

I is not the value, it is the index. " myTable == v". And its needed to get it again after we removed one value from the table because so every item in the table after the removed one "went back" one step, and so it can check that the new value that is now on the place of the removed one should be also removed or not ( in this case go to the next value).

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...