Movatterモバイル変換


[0]ホーム

URL:


CFEngine documentation homepage

maparray

Suggest changes
Table of contents

Prototype:maparray(pattern, array_or_container)

Return type:slist

Description: Returns a list with eacharray_or_container elementmodified by apattern.

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 valueof the current element, similar to the waythis is available formaplist.

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 theexample below for an illustration.

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

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

Arguments:

  • pattern:string - Pattern based on $(this.k) and $(this.v) as original text - in the range:.*
  • array_or_container:string - CFEngine variable identifier or inline JSON, the array variable to map - in the range:.*

Example:

code
bodycommoncontrol{bundlesequence=>{"run"};}bundleagentrun{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:

code
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: Thecollecting function behavior was added in 3.9. The delayed evaluation behavior was introduced in 3.10.

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

Still need help?

Chat Ask a question on Github Mailing list
Version 
master3.24 (LTS)3.21 (LTS)view all versions

[8]ページ先頭

©2009-2025 Movatter.jp