Using Key/Value Pairs

Key/value pairs are another important concept when it comes to creating Prog commands. It is also a useful technique to avoid having to create unnecessarily lengthy Prog step sequences.

In combination of “For Each” loops, you can design a command that iterates through a set of key/value pairs and dynamically assign variables within each loop as it is executed.

What is a key/value pair?

In programming, a key/value pair is a fundamental concept used to store and organize data. It’s like having a dictionary where each word (the key) is associated with a definition (the value).

For example, think of a contacts list on your phone. Each contact has a name (the key) and a phone number (the value). When you want to call a friend, you look up their name (the key) to find their phone number (the value).

In programming, key/value pairs are used in data structures like dictionaries, maps, or hash tables. They allow you to quickly access and manipulate data based on its unique identifier (the key), making it efficient to work with large amounts of information.

Use the Assign Key/Value Pair directive

Refer to the Assign Key/Value Pair Directive for more information.

Here is what it looks like to assign key/value pairs as part of your Prog command:

Assign Key/Value pairs form
Assign key/value pairs from this form

Use Runtime Variables to define Key/Value Pairs

It is also possible to define the key/value pairs at the time of running your Prog command.

See here for details on how runtime variables work.

How Key/Value Pairs are used in Proggy

Key/Value Pairs are primarily used in Proggy as part of “For Each” Loops. That is, if you want to iterate through a list where it would be convenient to be able to reference two separate but related pieces of information.

For an example of using Key/Value Pairs within loops, check out official/demo:foreach-loop-kv

You can also use Key/Value Pairs manually within directives using the Liquid template language. However, it is often easier to use Proggy’s built-in loop functionality to accomplish the same outcome.

For an example of using Key/Value Pairs within Liquid, check out official/demo:kv

Best Practices

It should be noted that it is best practices to define your key/value pairs 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.

In addition to this, it is recommended to keep your key/value list small enough to minimize excessively long loops – if you are using the “For Each” loop, for example.