maparray

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.

array_or_container can be a data container.

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: [a-zA-Z0-9_$(){}\[\].:]+

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);

      "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)";
      "mapped container: $(mapc_str)";
}

Output:

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 array: key='1', static lookup = 'lookup 2', value='2'
R: mapped container: { "key='top', key2='x', static lookup = 'lookup 2', value='2'", "key='top', key2='y', static lookup = 'lookup big', value='big'" }

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