RemoveItem() not working as i want.

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

RemoveItem() not working as i want.

Post by Bugala »

First of all Andreas, please update the manual explanation of RemoveItem command. I have so far thought I understood how that command works, and now im not sure anymore. Especially because the example makes no sense.

You mention in the example "Is removed because 8 is there twice". As far as i looked that code, I didnt get wether you meant that you yourself decided to remove the second 8 (spot 7 being number 8 with starting from 0 logic), or if you meant that the example program was somehow checking if there were doubles and removed the other.

Change them to using a,b,c,d,e,f,g,h,h,i and number 7 rather than using numbers twice, that makes it condusing.



Anyway, my problem is following. after hour of bug hunting, i found out this is the problem spot.
I have this piece of code:

Code: Select all

RemoveItem(at.tasks, n)

at.tasks is a table holding tables ie.

Code: Select all

at.tasks[1] = {stuff}
at.tasts[2] = {stuff}

At this crash point only one table exists in this at.tasks table, the table at.tasks[1] and n is 1.

So effectively it shoud destroy the at.tasks[1] table out.

However, this does not happen.

I have tried following:

Code: Select all

RawGet(at.tasks, 2)  - Not true
RawGet(at.tasks, 1) - True
RemoveItem(at.tasks, 1) - I see no reason why this would fail, so i suppose it happened
RawGet(at.tasks, 1) - True

How is it possible that right after checking that there is no other at.tasks[n] tables existing (checking number 2 and it failing) and checking that number 1 indeed exists, then how come when i delete number 1 out, and right after that in next line check if its existing, it tells me it does exist?

Shouldnt it have been removed, or did i misunderstand something about this command?
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: RemoveItem() not working as i want.

Post by jalih »

Bugala wrote: You mention in the example "Is removed because 8 is there twice". As far as i looked that code, I didnt get wether you meant that you yourself decided to remove the second 8 (spot 7 being number 8 with starting from 0 logic), or if you meant that the example program was somehow checking if there were doubles and removed the other.
It doesn't do any checking, only removes the specified item if it exist.
Bugala wrote: at.tasks is a table holding tables ie.

Code: Select all

at.tasks[1] = {stuff}
at.tasts[2] = {stuff}
At this crash point only one table exists in this at.tasks table, the table at.tasks[1] and n is 1.

So effectively it shoud destroy the at.tasks[1] table out.

However, this does not happen.
Remember, when using the list functions then the first index of your list table should be 0 instead of 1.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: RemoveItem() not working as i want.

Post by Bugala »

Im afraid using [0] table since im afraid of it causing me trouble at some point.

As example, if i use RawGet(table, n) and n = 0, then RawGet will result in NIL which means it doesnt exist, even if it would be existing, but is just holding 0 inside it.

So Jalih, is there some reason I should start from 0? That is there some problem that might arise from skipping the 0 table?
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: RemoveItem() not working as i want.

Post by jalih »

Bugala wrote:Im afraid using [0] table since im afraid of it causing me trouble at some point.

As example, if i use RawGet(table, n) and n = 0, then RawGet will result in NIL which means it doesnt exist, even if it would be existing, but is just holding 0 inside it.
Take a look at the GetType() function.
Bugala wrote:So Jalih, is there some reason I should start from 0? That is there some problem that might arise from skipping the 0 table?
Don't just take my word on it, but first examine this:

Code: Select all

Local table = {}

DebugPrint("Inserting table[1] = ONE")
InsertItem(table, "ONE", 1)
DebugPrint("Inserting table[2] = TWO")
InsertItem(table, "TWO", 2)
DebugPrint("Inserting table[3] = THREE")
InsertItem(table, "THREE", 3)

DebugPrint("Number of items in a list:", ListItems(table))
DebugPrint("table[1] = ", table[1])
DebugPrint("SUPRISE, we actually have table[0] = ", table[0])

DebugPrint("Traversing list...")
Local a, b = NextItem(table)
While GetType(a) <> #NIL
	DebugPrint(b)
	a, b = NextItem(table, a)
Wend
Post Reply