User Tools

Site Tools


crpl:docs:removelistelement

<- CRPL reference <- Lists

RemoveListElement

ArgumentsResultNotation
List, Index None L1 i1 –

Description

Removes the list element at location i1 in list L1, pushing the next element and all subsequent elements down by one.

Be careful when iterating over a list and using this command. If you delete list entries while looping over it, the deletions will move subsequent entries back, and the loop might fail to catch them all.

For example, if you try doing this:

CreateList ->list
"h" "e" 1 1 "l" "l" 1 "o" 1 "w" "o" "r" 1 "l" "d" 1 <-list AppendStackToList

<-list GetListCount 0 do
	<-list I GetListElement 1 eq if
		<-list I RemoveListElement
	endif
loop

The list output will combine into he1lloworld. This is because when the third element of the list was removed, the fourth element (which is also a 1) was moved to the third position, but the third position was already checked so it has been skipped over.

To fix this, you want to do this:

CreateList ->list
"h" "e" 1 1 "l" "l" 1 "o" 1 "w" "o" "r" 1 1 1 1 "l" "d" 1 <-list AppendStackToList

0 ->i
<-list GetListCount 0 do
	<-list <-i GetListElement 1 eq if
		<-list <-i RemoveListElement
		<-i 1 sub ->i
	endif

	<-i 1 add ->i
loop

Now, whenever we remove an element, we also push the current loop index back by 1, to make sure we catch all the elements inside the loop. The output combines into helloworld.

Examples

#Removes the 3rd element of list01
<-list01 2 RemoveListElement

Sample list list01 before:
0 - 100
1 - 105
2 - 110
3 - 115

Sample list list01 after sample command:
0 - 100
1 - 105
2 - 115

crpl/docs/removelistelement.txt · Last modified: 2021/02/05 11:44 by Karsten75