Using Arrays

Proggy uses the Liquid template language to allow users to inject dynamic values into their Prog commands at runtime.

In certain cases, you may desire to use an Array data structure as part of your Prog command.

For example:

  • You wish to iterate (eg. with a for each loop) through a list of values and execute the same step seqeunce for each of the value
  • You want to retrieve a list of values remotely, and want to format the values into an array so that you can present the data line-by-line

But before we go further, let’s explain what an Array is. If you are already informed about basic programming concepts, feel free to skip this next section.

What is an Array?

An array is a way to store multiple items in one place in programming. Think of it like a list where each item has a specific position.

For example, if you have an array of numbers like this:

numbers = [10, 20, 30, 40, 50]

Each number is stored in a position starting from 0. So, numbers[0] is 10, numbers[1] is 20, and so on.

Arrays can hold any type of item, like letters:

letters = ['a', 'b', 'c', 'd']

Or even words:

words = ['apple', 'banana', 'cherry']

Arrays help organize data and make it easy to perform tasks like sorting or searching through the items. They are a basic and powerful tool in programming.

Unfortunately, Liquid does not have permit the instantiation of Array data structures in the way you see above. Luckily with Proggy, you can use the Assign List directive to define your arrays.

Use the Assign List directive to define arrays

If you already know the content of an array, the Assign List directive is a good way to create an array which you can reference later in a variable.

The Assign List directive helps make it clear what your list should contain at the start of the Prog command.

The use of this directive, however, requires that you know the list before you execute your Prog command. To dynamically inject an array, it is possible with the use of runtime variables.

Use Runtime Variables to define arrays

See here for details on how runtime variables work.

Best Practices

It should be noted that it is best practices to define your list as early as possible in your Prog command, so it is clear to others who are using your Prog where these variables come from. Structuring it like this also allows for the possibility of overwriting these “defaults” when it is necessary.

See our recommended best practices for more details.