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

Description

The listutils:slice returns a slice of a list.

Usage

Input is given using the listutils:in storage:

Field Meaning
List The list that needs to be sliced.

Additionally, the smallest index (inclusive) should be put in the $listutils.index.first listutils.in score (defaults to 0). The largest index (exclusive) should be put in the $listutils.index.last listutils.in score (defaults to the length of the list). Negative indices will have the same behavior as using them in a static context.

After defining the input, run the function listutils:slice.

Errors

Errors that display when executing the listutils:slice function 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 either of the indices is out of bounds or when the first index is larger than the last index.

Result

The result of the operation is stored inside the List field in the listutils:out storage and contains the slice of the original list.

Example

Take the example list ExampleList: ["foo", "Hello World!", "foo", "bar"] in the listutils:examples storage. We want to slice it from index 1 to 3:

# Add List to the input.
data modify storage listutils:in List set from storage listutils:examples ExampleList
# Add the indices to the input.
scoreboard players set $listutils.index.first listutils.in 1
scoreboard players set $listutils.index.last listutils.in 3
# Call the function.
function listutils:slice

This would return ["Hello World!", "foo"] inside the List field in the listutils:out storage.

Clone this wiki locally