Prototype: intersection(list1, list2)

Return type: slist

Description: Returns the unique elements in list1 that are also in list2.

This function can accept many types of data parameters.

Arguments:

  • list1: string - CFEngine base variable identifier or inline JSON - in the range: .*
  • list2: string - CFEngine filter variable identifier or inline JSON - in the range: .*

Example:

code
body common control
{
      bundlesequence => { "test" };
}

bundle agent test
{
  vars:
      "a" slist => { 1,2,3,"x" };
      "b" slist => { "x" };

      "mylist1" slist => { "a", "b" };
      "mylist2" slist => { "a", "b" };
      "$(mylist1)_str" string => join(",", $(mylist1));

      "int_$(mylist1)_$(mylist2)" slist => intersection($(mylist1), $(mylist2));
      "int_$(mylist1)_$(mylist2)_str" string => join(",", "int_$(mylist1)_$(mylist2)");

  reports:
      "The intersection of list '$($(mylist1)_str)' with '$($(mylist2)_str)' is '$(int_$(mylist1)_$(mylist2)_str)'";
}

Output:

code
R: The intersection of list '1,2,3,x' with '1,2,3,x' is '1,2,3,x'
R: The intersection of list '1,2,3,x' with 'x' is 'x'
R: The intersection of list 'x' with '1,2,3,x' is 'x'
R: The intersection of list 'x' with 'x' is 'x'

See also: About collecting functions, difference().