[20 Aug 2006] Need Help Again with RemoveItem()

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
NathanH
Posts: 128
Joined: Sun Jul 05, 2015 1:29 am
Location: Caldwell, Idaho

[20 Aug 2006] Need Help Again with RemoveItem()

Post by NathanH »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Sun, 20 Aug 2006 11:47:00 -0600

Hi,

I'm dense so still don't understand the workings of RemoveItem(). I'm trying to dynamically hold a history of the last "History" items in a table via the following idealized code. I expected that this code would keep the last five item ids. If the debug output is reviewed, however, a copy of the 4th item is somehow created and the table grows until memory runs out. Please help me understand why this happens. Thanks!!!!

Nathan

Code: Select all

History=5
tblHistory={}

function p_Add2History()
    local count=listitems(tblHistory)
    local junk=0

    if count=history
        debugprint("Count = ",history)
        junk=removeitem(tblHistory,0)
    endif
    tblHistory[count]={}
    tblHistory[count].id=count

    debugprint("Before=",count,"After=",listitems(tblHistory))

    for local i=0 to listitems(tblHistory)-1
        debugprint(tblHistory[i].id)
    next
endfunction

for j=1 to 7
    p_Add2History()
next

**Debug Output from above code**
Before= 0 After= 1
0
Before= 1 After= 2
0
1
Before= 2 After= 3
0
1
2
Before= 3 After= 4
0
1
2
3
Before= 4 After= 5
0
1
2
3
4
Count =  5
Before= 5 After= 4 ** expected this would be 5,5 **
1
2
3
4  ** expected that this would be 1,2,3,4,5 **
Before= 4 After= 6 ** expected this would be 5,5 **
1
2
3
4
4
5 ** expected this would be 2,3,4,5,5 **

User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[24 Aug 2006] Re: Need Help Again with RemoveItem()

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Thu, 24 Aug 2006 23:31:34 +0200
Hi,

I'm dense so still don't understand the workings of RemoveItem(). I'm trying to dynamically hold a history of the last "History" items in a table via the following idealized code. I expected that this code would keep the last five item ids. If the debug output is reviewed, however, a copy of the 4th item is somehow created and the table grows until memory runs out. Please help me understand why this happens. Thanks!!!!

Nathan

Code: Select all

History=5
tblHistory={}

function p_Add2History()
   local count=listitems(tblHistory)
   local junk=0

   if count=history
       debugprint("Count = ",history)
       junk=removeitem(tblHistory,0)
   endif
   tblHistory[count]={}
   tblHistory[count].id=count
Be careful here. If count is 5, you remove the first item of tblHistory. So your table will have 4 entries at indices 0,1,2,3 now. The variable "count" however is still 5. Thus, the new item in the next line of your code will be inserted at index 5. But this insertion will break your list because index 4 is NIL and all the list functions of Hollywood (RemoveItem, ListItems etc.) will stop at a NIL index.

I don't really know what you're trying to achieve with that code but this is an obvious bug that I see in the posted code.
NathanH
Posts: 128
Joined: Sun Jul 05, 2015 1:29 am
Location: Caldwell, Idaho

[02 Sep 2006] Re: Need Help Again with RemoveItem()

Post by NathanH »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Sat, 2 Sep 2006 19:21:31 -0600

Hi,

I'm back from my hiatus. I guess a clearer example of my problem is:

Code: Select all

mytable={}
debugprint (listitems(mytable))
mytable[0]=3
debugprint (listitems(mytable))
stuff=removeitem(mytable,0)
debugprint (listitems(mytable))
repeat
    waitevent
forever
end
Gives the output: 0 1 1

Why isn't it: 0 1 0

Thanks.

Nathan
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[03 Oct 2006] Re: Need Help Again with RemoveItem()

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Tue, 03 Oct 2006 14:01:38 +0200

Very good question. It really should be 0 1 0. This is obviously a bug. RemoveItem() in 2.0 does not allow you to remove the last remaining element of a table. I just fixed it. Thanks for the report.
NathanH
Posts: 128
Joined: Sun Jul 05, 2015 1:29 am
Location: Caldwell, Idaho

[09 Oct 2006] Re: Need Help Again with RemoveItem()

Post by NathanH »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 9 Oct 2006 10:29:15 -0600

Thanks dude!
Locked