Table index and Nil

Discuss any general programming issues here
Post Reply
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Table index and Nil

Post by Flinx »

Due to an error in my program I appended an empty element to a table. As a result, the integer indexes (or the result of TableItems) are no longer correct. I am not sure if I should call this a bug.

Code: Select all

Test=CreateList()
For i=1 To 3
	InsertItem(Test, i)
Next
ForEach(Test,DebugPrint)
DebugPrint("Last item:",Test[TableItems(Test)-1])

DebugPrint("\nAdd ",emptyvar)
InsertItem(Test, emptyvar)
ForEach(Test,DebugPrint)

DebugPrint("\nAdd item")
InsertItem(Test, 8)
ForEach(Test,DebugPrint)
DebugPrint("Last item:",Test[TableItems(Test)-1])
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Table index and Nil

Post by SamuraiCrow »

Probably not a bug because assigning NIL to a table entry is the legal way to mark it as empty.
I'm on registered MorphOS using FlowStudio.
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Table index and Nil

Post by Flinx »

SamuraiCrow wrote: Sat Dec 31, 2022 7:06 am Probably not a bug because assigning NIL to a table entry is the legal way to mark it as empty.
But look at the third to last and the last line. An element has been added to the end of the table, and then the last element of the table is accessed. This should not fail, I think.
User avatar
jPV
Posts: 734
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Table index and Nil

Post by jPV »

Maybe it gets messed, because it's not allowed to remove items from an optimized list (CreateList()) by anything else but RemoveItem(). So removing an item by setting it to Nil is against the guideline.

And in any case when one item (index 3) has been removed by setting it to Nil but index counter probably still increased (with an optimized list? Doesn't do it with a "normal" list?), there's four items left, and the last line tries to print the item at index 3 then...
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Table index and Nil

Post by airsoftsoftwair »

Not really a bug because you're using TableItems() with an optimized list which is not really supported. For optimized lists you should always use ListItems() to get the number of items. Using TableItems() for lists doesn't make sense anyway because a list shouldn't contain anything else than just a linear array of 0..n items.
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Table index and Nil

Post by Flinx »

Thank you for the advice. Perhaps this should be said a little more explicitly in the manual.
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Table index and Nil

Post by airsoftsoftwair »

Flinx wrote: Tue Jan 03, 2023 4:25 pm Thank you for the advice. Perhaps this should be said a little more explicitly in the manual.
Well, I've just changed TableItems() to behave like ListItems() now in case the table passed was instantiated by CreateList(). That's probably the most convenient solution.

Code: Select all

- Change: For optimized lists created using CreateList(), TableItems() behaves the same as ListItems() now
Post Reply