maparray

Table of Contents
Table of Contents

Prototype: maparray(pattern, array_or_container)

Return type: slist

Description: Returns a list with each array_or_container element modified by a pattern.

This function can accept many types of data parameters.

This function can delay the evaluation of its first parameter, which can therefore be a function call.

The $(this.k) and $(this.v) variables expand to the key and value of the current element, similar to the way this is available for maplist.

If the array has two levels, you'll also be able to use the $(this.k[1]) variable for the key at the second level. See the example below for an illustration.

If a value in the array is an slist, you'll get one result for each value (implicit looping).

The order of the array keys is not guaranteed. Use the sort function if you need order in the resulting output.

Arguments:

  • pattern: string, in the range: .*
  • array_or_container: string, in the range: .*

Example:

body common control
{
      bundlesequence => { "run" };
}

bundle agent run
{
  vars:
      "static[2]" string => "lookup 2";
      "static[two]" string => "lookup two";
      "static[big]" string => "lookup big";
      "static[small]" string => "lookup small";

      "todo[1]" string => "2";
      "todo[one]" string => "two";
      "todo[3999]" slist => { "big", "small" };
      "map" slist =>
          maparray("key='$(this.k)', static lookup = '$(static[$(this.v)])', value='$(this.v)'",
                   todo);
      "map_sorted" slist => sort(map, lex);

      "mycontainer" data => parsejson('
{
  "top":
  {
    "x": 2,
    "y": "big"
  }
}');
      "mapc" slist =>
          maparray("key='$(this.k)', key2='$(this.k[1])', static lookup = '$(static[$(this.v)])', value='$(this.v)'",
          mycontainer);
      "mapc_str" string => format("%S", mapc);

  reports:
      "mapped array: $(map_sorted)";
      "mapped container: $(mapc_str)";
}

Output:

R: mapped array: key='1', static lookup = 'lookup 2', value='2'
R: mapped array: key='3999', static lookup = 'lookup big', value='big'
R: mapped array: key='3999', static lookup = 'lookup small', value='small'
R: mapped array: key='one', static lookup = 'lookup two', value='two'
R: mapped container: { "key='top', key2='x', static lookup = 'lookup 2', value='2'", "key='top', key2='y', static lookup = 'lookup big', value='big'" }

History: The collecting function behavior was added in 3.9. The delayed evaluation behavior was introduced in 3.10.

See also: maplist(), mapdata(), about collecting functions, and data documentation.