Skip to content
PeerHeer edited this page Dec 31, 2019 · 4 revisions

Description

The Delete operation finds the index of a data element. In List Utils, there are 4 functions for Delete:

  • The listutils:delete_index function deletes an element at an index.
  • The listutils:delete_last function deletes the last matching element.
  • The listutils:delete_first function deletes the first matching element.
  • The listutils:delete_all function deletes all matching elements. It is recommended to use the listutils:delete_last function instead of the listutils:delete_first function, because the former has a more efficient implementation.

Usage

Input

Using listutils:delete_last, listutils:delete_first and listutils:delete_all

Input is given using the listutils:in storage:

Field Meaning
List The list that contains the element.
Data The element to delete.

Using listutils:delete_index

When using the listutils:delete_index function, Data is not used. Instead, the $listutils.index listutils.in score should be used to pass the index to be deleted to the function. List should still be used as a list that contains the index to delete.

After defining the input, run one of the listutils:delete functions.

Comparison functions

Various comparison functions can be used to match elements. For more about comparison functions, see the Comparison Functions page.

Errors

Errors that display when executing the listutils:delete functions as a player in debug mode.

Error Message
TBD TBD

Return values

Success

The success of the operation is stored in the $listutils.success listutils.out score. This score is 1 on success and 0 otherwise. Success will be 0 when the list does not contain the element to delete or the index to delete is out of bounds.

Result

The result of the operation is stored in the List field in the listutils:out storage. This result contains the list with matching element(s) deleted.

Additionally, the $listutils.result listutils.out score contains the amount of elements deleted when using delete_all.

Example

Take the example list ExampleList: ["foo", "Hello World!", "foo", "bar"] in the listutils:examples storage. We want to delete all occurrences of "foo":

# Add List to the input.
data modify storage listutils:in List set from storage listutils:examples ExampleList
# Add Data to the input.
data modify storage listutils:in Data set value "foo"
# Call the function.
function listutils:delete_all

This would return 2 as the result in $listutils.result listutils.out (deleted 2 elements) and would yield the list ["Hello World!", "bar"].

Clone this wiki locally