IMPORTANT NOTICE: As of 21 February, anything posted in this community WILL NOT BE MIGRATED to our new community site. We have pulled all user information and data from this site and are now in an approximately week long process of importing users, roles, and data to our new site. We are leaving this site open and active so you can post and hopefully get a response until the migration is complete. Once complete, the URL that currently takes you to this site will take you to our new site so your bookmarks will work as always. Please read the information on a New Login Process
The code below is intended to delete the entire contents of lsListofObjects in an incremental order from first to last:
[-] testcase one () appstate none [-] list of string lsListofObjects = {...} [ ] "a" [ ] "b" [ ] "c" [ ] "d" [ ] "e" [ ] listprint(lsListofObjects) [ ] [ ] integer x=100, a [-] for a=1 to x [ ] ListDelete(lsListofObjects, a) [ ] print ("Deleting Item: "+str(a)) [ ] listprint(lsListofObjects)
However, as we can see from the results, the items are not being deleted in the way expected and we are receiving an indexing error after only 3 deletions:
[ ] a [ ] b [ ] c [ ] d [ ] e [ ] Deleting Item: 1 [ ] b [ ] c [ ] d [ ] e [ ] Deleting Item: 2 [ ] b [ ] d [ ] e [ ] Deleting Item: 3 [ ] b [ ] d [ ] *** Error: Index value of 4 exceeds list size of 2 [ ] Occurred in ListDelete [ ] Called from one at listdelete.t(12)
The reason that this occurs is because every time an item is deleted from the list, the list is depleted by one and the value at position 1 changes with each deletion. As you go through the list from beginning to end and delete each of the items, it will remove the 1st item, moving the 2nd item into the first position. Then it will remove the 2nd item which was originally the 3rd item, and so on.....
All this means is that you cannot use an incremental index as a reliable format from which to make our your through the list as you delete.
What you should do is to always delete either the First or Last item in the list every time it goes through the loop.
For example, if you decide to always delete the first item then as the value at Index 1 is deleted, the next value is placed in this position to be deleted the next time the loop is executed. Thereby deleting the entire list in incremental order from beginning to end.
Here are two examples of how to code this:
[-] for i = 1 to ListCount (lsList) // always delete the first item [ ] ListDelete (lsList, 1)
OR
[-] for i = ListCount (lsList) to 1 step -1 // always delete the last item [ ] ListDelet (lsList, i)