Skip to content
PeerHeer edited this page Dec 31, 2019 · 1 revision

Description

The listutils:sum function sums all elements in a list according to their data get value.

Usage

Input is given using the listutils:in storage:

Field Meaning
List The list to sum the elements of.

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

Errors

Errors that display when executing the listutils:sum 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. By default, the success is always 1 when not running in debug mode.

Result

The result of the operation is stored in the $listutils.result listutils.out score and contains the final sum of all elements.

Example

Take the example list ExampleList: ["foo", "Hello World!", "foo", "bar"] in the listutils:examples storage. We want to take the average length of a string in the list:

# Get list length.
execute store result score $length var run data get storage listutils:examples ExampleList
# Add List to the input.
data modify storage listutils:in List set from storage listutils:examples ExampleList
# Call the function.
function listutils:sum
# Divide the result by the length of the list.
scoreboard players operation $listutils.result listutils.out /= $length var

This would return 30 in the $listutils.result listutils.out score as the result of the operation. The final average is 7.

Clone this wiki locally