Prototype: some(regex, list)

Return type: boolean

Description: Return whether any element of list matches the Unanchored regular expression regex.

This function can accept many types of data parameters.

Arguments:

  • regex: regular expression - Regular expression or string - in the range: .*
  • list: string - CFEngine variable identifier or inline JSON - in the range: .*

some() will return as soon as any element matches.

It's convenient to set a class to not => some(".*", mylist) in order to check if mylist is empty. Since some() returns as soon as possible, that is better than using length() or every() or none() which must traverse the entire list.

Example:

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

bundle agent test
{
  classes:

      # This is an easy way to check if a list is empty, better than
      # expression => strcmp(length(x), "0")

      # Note that if you use length() or none() or every() they will
      # go through all the elements!!! some() returns as soon as any
      # element matches.
      "empty_x" not => some(".*", x);
      "empty_y" not => some(".*", y);

      "some11" expression => some("long string", test1);
      "some12" expression => some("none", test1);
      "some21" expression => some("long string", test2);
      "some22" expression => some("none", test2);

  vars:
      "x" slist => { "a", "b" };
      "y" slist => { };

      "test1" slist => {
                        1,2,3,
                        "one", "two", "three",
                        "long string",
                        "four", "fix", "six",
                        "one", "two", "three",
      };


      "test2" data => parsejson('[1,2,3,
                        "one", "two", "three",
                        "long string",
                        "four", "fix", "six",
                        "one", "two", "three",]');

  reports:
    empty_x::
      "x has no elements";
    empty_y::
      "y has no elements";

    any::
      "The test1 list is $(test1)";
    some11::
      "some() test1 1 passed";
    !some11::
      "some() test1 1 failed";
    some12::
      "some() test1 2 failed";
    !some12::
      "some() test1 2 passed";

      "The test2 list is $(test2)";
    some21::
      "some() test2 1 passed";
    !some21::
      "some() test2 1 failed";
    some22::
      "some() test2 2 failed";
    !some22::
      "some() test2 2 passed";

}

Output:

code
R: y has no elements
R: The test1 list is 1
R: The test1 list is 2
R: The test1 list is 3
R: The test1 list is one
R: The test1 list is two
R: The test1 list is three
R: The test1 list is long string
R: The test1 list is four
R: The test1 list is fix
R: The test1 list is six
R: some() test1 1 passed
R: some() test1 2 passed
R: The test2 list is 1
R: The test2 list is 2
R: The test2 list is 3
R: The test2 list is one
R: The test2 list is two
R: The test2 list is three
R: The test2 list is long string
R: The test2 list is four
R: The test2 list is fix
R: The test2 list is six
R: some() test2 1 passed
R: some() test2 2 passed

History: The collecting function behavior was added in 3.9.

See also: About collecting functions, filter(), every(), and none().