-
Notifications
You must be signed in to change notification settings - Fork 0
Sum
The listutils:sum function sums all elements in a list according to their data get value.
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 that display when executing the listutils:sum function as a player in debug mode.
| Error | Message |
|---|---|
| TBD | TBD |
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.
The result of the operation is stored in the $listutils.result listutils.out score and contains the final sum of all elements.
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 varThis would return 30 in the $listutils.result listutils.out score as the result of the operation. The final average is 7.