CFEngine: new sort(), reverse(), shuffle() functions
  2013-05-27

These slist functions will be part of the upcoming 3.5 release. For each function, a small downloadable example:

Sort

Sort slist elements, at the moment only lexicographical sort is supported (“lex” argument):

sort.cf

body common control { bundlesequence => { "my_sort" }; } bundle agent my_sort { vars: "countries" slist => { "bulgaria", "cambodia", "malaysia", "australia", "canada" }; "countries_sorted" slist => sort("countries", "lex"); # Slist to string for reports "countries_str" string => join (" ", "countries"); "countries_sorted_str" string => join (" ", "countries_sorted"); reports: "Countries: $(countries_str)"; "Sorted countries: $(countries_sorted_str)"; }

Output:

$ cf-agent -KIf sort.cf
2013-05-27T20:09:57+0200   notice: R: Countries:        bulgaria cambodia malaysia australia canada
2013-05-27T20:09:57+0200   notice: R: Sorted countries: australia bulgaria cambodia canada malaysia

Reverse

Reverse the order of slist elements:

reverse.cf

body common control { bundlesequence => { "my_reverse" }; } bundle agent my_reverse { vars: "countries" slist => { "australia", "bulgaria", "cambodia", "canada", "malaysia" }; "countries_reversed" slist => reverse("countries"); # Slist to string for reports "countries_str" string => join (" ", "countries"); "countries_reversed_str" string => join (" ", "countries_reversed"); reports: "Countries: $(countries_str)"; "Reversed countries: $(countries_reversed_str)"; }

Output:

$ cf-agent -KIf reverse.cf
2013-05-27T20:02:30+0200   notice: R: Countries:          australia bulgaria cambodia canada malaysia
2013-05-27T20:02:30+0200   notice: R: Reversed countries: malaysia canada cambodia bulgaria australia

Shuffle

Shuffle slist elements, given a random seed:

shuffle.cf

body common control { bundlesequence => { "my_shuffle" }; } bundle agent my_shuffle { vars: "countries" slist => { "australia", "bulgaria", "cambodia", "canada", "malaysia" }; "countries_shuffled_0" slist => shuffle("countries", "my seed"); "countries_shuffled_1" slist => shuffle("countries", "another seed"); # Slist to string for reports "countries_str" string => join (" ", "countries"); "countries_shuffled0_str" string => join (" ", "countries_shuffled_0"); "countries_shuffled1_str" string => join (" ", "countries_shuffled_1"); reports: "Countries: $(countries_str)"; "Shuffled countries 0: $(countries_shuffled0_str)"; "Shuffled countries 1: $(countries_shuffled1_str)"; }

Output:

$ cf-agent -KIf shuffle.cf
2013-05-27T20:17:16+0200   notice: R: Countries:            australia bulgaria cambodia canada malaysia
2013-05-27T20:17:16+0200   notice: R: Shuffled countries 0: cambodia bulgaria malaysia australia canada
2013-05-27T20:17:16+0200   notice: R: Shuffled countries 1: canada malaysia australia bulgaria cambodia