Parsing an array

Is used when we want to read/modify an element of an array (Basically do data get/modify ... Array[n] where n is a variable )

There are a couple of methods, so use tabs above the page to find the one that suits you the best!

Method 1

This method works for both reading & modifying arrays. It is efficient, short & doesn't change element order, but can't be directly used on player NBT.

  1. Store the initial length of the array;
  2. Get to Read/Compare/Change the first element of the array;
  3. Move the first element of the array to the end;
  4. Repeat until the counter is 0;
File:
execute store result score #Counter run data get execute if score #Counter matches 1.. run function parse/step
scoreboard players remove #Counter 1
# At this line we can do with the element whatever we want, print it, check it, modify and more by using: # data get [0]
data modify append from [0] data remove [0]
execute if score #Counter matches 1.. run function parse/step
Define a path where this algorithm will be executed Define a scoreboard you will be using Define the NBT Path that you will be parsing

Method 2

This method is an old fashion, that was replaced with Method 1.

We are creating a copy of our target, then we get its last element [-1], we can do any checks with this element, then we remove it and go through this copy until it's empty.

File:
# 1. We need to create a temporary data holder, which we can modify without losing any data: data modify .Parsing set from
# 2. We create a counter, that will start with the size of the array and decrease in a loop, as well as a result placeholder: execute store result score #Counter run data get data remove .Match
# 3. Then if the array isn't empty we start a loop: execute if score #Counter matches 1.. run function step
scoreboard players remove #Counter 1
# At this point we can read the element by doing: # data get .Parsing[-1]
# 4. We remove the last element of the array data remove .Parsing[-1]
# Repeat the function or return the field depending on the counter: execute if score #Counter matches ..0 run data modify .Match set from .Parsing[-1] execute if score #Counter matches 1.. run function step
Define a path where this algorithm will be executed Define a scoreboard you will be using Define the NBT Path that you will be parsing Define the temporary NBT Path