Moderators IIYAMA Posted February 21, 2013 Moderators Share Posted February 21, 2013 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
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 for i, v in ipairs ( myTable ) do while not isElement( myTable[ i ] ) do table.remove( myTable, i ) end end Link to comment
Moderators IIYAMA Posted February 21, 2013 Author Moderators Share Posted February 21, 2013 But it will still skip some data? Link to comment
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 No, I don't think it will skip any. Link to comment
Moderators IIYAMA Posted February 21, 2013 Author Moderators Share Posted February 21, 2013 airplanes_weapon\server.lua:58 Aborting; infinite running script in airplanes_weapons but it will go on for ever. Link to comment
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 Hm, I made a few tests with it and worked perfectly. So lets find out an another method.. Link to comment
Moderators IIYAMA Posted February 21, 2013 Author Moderators Share Posted February 21, 2013 Well I think I let the inpairs loop twice, Link to comment
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 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
Anderl Posted February 21, 2013 Share Posted February 21, 2013 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
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 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 IIYAMA Posted February 21, 2013 Author Moderators Share Posted February 21, 2013 (edited) 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 February 21, 2013 by Guest Link to comment
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 (edited) Ahh, i messed up a bit the variables sorry, will fix it. edit: for i, v in ipairs ( myTable ) do while myTable[ i ] and not tonumber( myTable[ i ] ) do table.remove( myTable, i ) end end Edited February 21, 2013 by Guest Link to comment
Anderl Posted February 21, 2013 Share Posted February 21, 2013 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
Moderators IIYAMA Posted February 21, 2013 Author Moderators Share Posted February 21, 2013 I forgot this: my element I need to check is here: v[1] v[2] = weapon1 v[3] = weapon2 element, weapon1, weapon2 = unpack(v) is this possible? (myTable[ i ])[1] Link to comment
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 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 IIYAMA Posted February 21, 2013 Author Moderators Share Posted February 21, 2013 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
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 What's this? (weaponObjectsVeh)[1] ? Is it working so? The [1] isn't needed. And what do you mean by smaller? I don't have a better idea, sorry Link to comment
Moderators IIYAMA Posted February 21, 2013 Author Moderators Share Posted February 21, 2013 (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
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 So v is an another table, i got it, and of course: you are welcome. Link to comment
Anderl Posted February 21, 2013 Share Posted February 21, 2013 Third time I repeat this: You do not need to call the index from the array (I mean this: myTable, it SHOULD be simply i)! Link to comment
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 Could you please explain why not? myTable != i Link to comment
Anderl Posted February 21, 2013 Share Posted February 21, 2013 Because i is already the value, there is no need to get it again ( I also did some tests on the LUA interpreter and the condition "table == i" was false. Link to comment
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 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
Anderl Posted February 21, 2013 Share Posted February 21, 2013 I was not talking about array values. I was saying 'i' was already the value from the array. Link to comment
csiguusz Posted February 21, 2013 Share Posted February 21, 2013 Sorry I still don't understand. '"i' was already the value from the array?" Hm... In my for loop 'i' was the index and 'v' was the value of the table (array?). Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now