Page 1 of 1
Table index and Nil
Posted: Mon Dec 26, 2022 6:49 pm
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])
Re: Table index and Nil
Posted: Sat Dec 31, 2022 7:06 am
by SamuraiCrow
Probably not a bug because assigning NIL to a table entry is the legal way to mark it as empty.
Re: Table index and Nil
Posted: Sat Dec 31, 2022 12:42 pm
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.
Re: Table index and Nil
Posted: Sat Dec 31, 2022 9:04 pm
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...
Re: Table index and Nil
Posted: Mon Jan 02, 2023 10:16 pm
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.
Re: Table index and Nil
Posted: Tue Jan 03, 2023 4:25 pm
by Flinx
Thank you for the advice. Perhaps this should be said a little more explicitly in the manual.
Re: Table index and Nil
Posted: Tue Jan 03, 2023 10:15 pm
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