Extension:ArrayFunctions/Essentials/Creating an array

Arrays can be created in two ways, either directly in wikitext or through Lua. In this chapter, we will focus on creating arrays using wikitext. For instructions on creating arrays using Lua with Scribunto, please refer to this chapter.

Lists and objects

edit

We distinguish between lists and objects. While both are referred to as arrays, they are used slightly differently:

  • A list is an ordered collection of items with a numeric index.
  • An object is an unordered collection of items with a string index.

ArrayFunctions provides two fundamental parser functions for creating arrays:

  • #af_list creates a list
  • #af_object creates an object.

Using these parser functions is fairly straightforward. Below are examples of how to use each:

Creating a list
{{#af_list: item1 | item2 | item3 }}
Creating an object
{{#af_object: key1 = item1 | key2 = item2 | key3 = item3 }}

Displaying an array

edit

If you try one of the examples above, you might have be puzzled by the result. Instead of a nicely formatted array, you see an unreadable string. This happens because ArrayFunctions encodes the array before outputting it. This encoding is done for a number of reasons:

  • The encoded string will not be touched by MediaWiki, ensuring that arrays are not accidentally altered by the parser.
  • It creates an opaque type, ensuring all computation and manipulations happen through ArrayFunctions. This approach allows for greater flexibility in how values are internally managed and updated.

To display the contents of an array, you need to use other ArrayFunctions functions designed to decode and output the data in a human-readable manner. Here is an example of how you can display the contents of a list or object:

{{#af_print: {{#af_list: item1 | item2 | item3 }} }}
{{#af_print: {{#af_object: key1 = item1 | key2 = item2 | key3 = item3 }} }}
Essentials: Creating an array Essentials: Composing functions