Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

When using ListDelete(), the list is not deleted in the order I would expect and I receive an indexing error. Why is this and how do I fix it?

When using ListDelete(), the list is not deleted in the order I would expect and I receive an indexing error. Why is this and how do I fix it?

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)

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:26
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.