Page 1 of 1

Tables not working right

Posted: Sat Nov 09, 2013 10:56 pm
by LuaMaster
Okay, right off the bad, please do NOT point out crashes, bugs, or inefficient things unless they are DIRRECTLY related to the issue I'm having. I know where the crashes are. I will get to them later.

Anyhow, so I'm trying to delete object 2. But if there is a 3rd object there, it will ALWAYS delete the 3rd, even though I tell it to delete the 2nd.
How to reproduce:
Make three objects by puting the cursor in the positon then pressing p
Press D
Click deletemode
Press 2
Press enter
Wait
Then see that it deleted the object with the three above it.
Use the program I've attached in the post below and put the FOLDER named archive.love in your love folder.

Re: Tables not working right

Posted: Sat Nov 09, 2013 11:02 pm
by LuaMaster
Sorry I couldn't upload it, but you can use pastebin
Steps:
Get the program from pastebin.com/Gwc5Zxme
Paste it into a file called main.lua
Zip main.lua
Call it Archive.love
Then get this:
http://www.sendspace.com/file/iq6x8v
Unzip it, then place the contents in your LOVE folder.
Thanks for helping.

Re: Tables not working right

Posted: Sat Nov 09, 2013 11:32 pm
by Ranguna259
I don't really recomend on using table.remove, use this instead:

Code: Select all

for i=numbertodeletefromtable,#table do
  table[i]=table[i+1]
end
Exemple usage:

Code: Select all

table={}
table[1]=1
table[2]=2
table[3]=3


for i=2,#table do
  table[i]=table[i+1]
end
EDIT: Don't worry that code wont go slow on large table, I managed to get 900+ FPS on a table of 800+ arrays, so it's good.

Why in hell's name don't you just post the love file, is it that hard ?
http://www.mediafire.com/download/7z6n6 ... elGUI.love
Guys use this file if you want to check out the OP's code ^

Re: Tables not working right

Posted: Sun Nov 10, 2013 1:44 am
by LuaMaster
I had to upload the archive.love so you can actually run it
And what's the difference between the two?
Also, I really don't care about slowness. The program I'm using is meant to get a job done, but nothing else. Not to run fast or to look nice, etc. etc.
EDIT: I've changed the way it does it, now it gives me another error-- but it doesn't make any sence because I have set delNum to 2 yet it says it isn't a number. Help.(link, in love file: http://www.sendspace.com/file/0iugo5 )
Nevermind, I got it working. I just changed the variable name in a few spots but not all and it works fine now. And yes, I know, it's going to get slower and slower as I do that kind of stuff. It's okay.

Re: Tables not working right

Posted: Sun Nov 10, 2013 4:46 am
by Robin
Ranguna259 wrote:I don't really recomend on using table.remove, use this instead:
Whyyyyy? What reason can you possibly have to do that?

Re: Tables not working right

Posted: Sun Nov 10, 2013 11:36 am
by Ranguna259
Robin wrote:
Ranguna259 wrote:I don't really recomend on using table.remove, use this instead:
Whyyyyy? What reason can you possibly have to do that?
From here:
because it will move items several times when only one is needed. For instance if you remove every other entry in the array then a quadratic number of moves will be performed if you use table.remove

Re: Tables not working right

Posted: Sun Nov 10, 2013 12:21 pm
by micha
Ranguna259 wrote:
because it will move items several times when only one is needed. For instance if you remove every other entry in the array then a quadratic number of moves will be performed if you use table.remove
Your code
Ranguna259 wrote:

Code: Select all

for i=numbertodeletefromtable,#table do
  table[i]=table[i+1]
end
Does the same. It moves several items around. And if you need to keep the order in the array, then I don't see a simple way around (maybe you can leave gaps).

If you don't care about the order, then you can replace the deleted entry by the last one.

Code: Select all

function remove(table,index)
  table[index] = table[#table]
  table[#table] = nil
end