PDA

View Full Version : Problem removing item from the end of a list.


DrJim
02-22-2007, 02:37 PM
I have a problem using the “remove item from end of <list>“ command - when I try to use it, I always get an error message. The message is similar to that I get when I try to remove something from a list and what I’m trying to remove isn’t there - but it seems like there should always be an item at the end of the list unless the list is completely empty.

The attached world illustrates the problem. As set up, it works fine - the bunny moves from cone to cone around a circle, going to one less cone each loop.

However if I substitute “remove item from end ...” for “remove item from position ...” I get an error - even though these should be equivalent commands. Looks to me like this is a real bug - anyone see anything I’m missing?

chuck
02-22-2007, 07:54 PM
Hello Dr. Jim,

First, you are right. Remove item from end of (list name) doesn’t work properly – for list variables. It does work for a list created as a ListVisualization object.

I looked at the error code and found that an index out of bounds exception was being generated. I believe the underlying code is trying to delete item N from a list with N items. Since the list numbering starts with 0, it should be trying to delete item N-1 from a list with N items.

(This underlying problem is very reminiscent of things that happen in CS1 the first time students write their own programs to access arrays. I’ve also seen it in professional programs where good programmers simply were off by one. Fortunately, in this case NASA won’t lose a space probe because of a software error.)

Quick fix – to delete an item from the end of a list, delete the item from position [size of list] – 1. See the modified attached world.