bofh...@gmail.com writes:
> Not sure why this is happening. How come the first report in "my_split" is
> blank?
When you split something you end up with at least 2 parts. In this case
your splitting off the front of a string, so you end up with an empty
element (the first part), and then the second part as another element.
Since your only interested in the second part you can use [[
https://docs.cfengine.com/docs/3.10/reference-functions-nth.html][nth()]] to pick
it out.
#+BEGIN_SRC cfengine3 :exports both :wrap EXAMPLE
bundle agent main
{
vars:
"my_list" slist => { "xvnc10", "xvnc30", "xvnc40" };
# When you split a string, you will have at least 2 parts from splitting
# it. Since you are splitting off the front of the string you end up with
# an empty string (the first half of your split string) and then the digits
# in the second half. Since you are only interested in the second half of
# that split, you can pick it out using nth(). You might be able to do
# similar using data_regextract() or similar.
"my_split[$(my_list)]" string => nth( string_split( $(my_list), "^xvnc", 2), 1);
reports:
"CFEngine: $(sys.cf_version)";
"my_list $(my_list)";
"my_split $(my_split[$(my_list)])";
}
#+END_SRC
#+RESULTS:
#+BEGIN_EXAMPLE
R: CFEngine: 3.11.0a.334d1fc
R: my_list xvnc10
R: my_list xvnc30
R: my_list xvnc40
R: my_split 10
R: my_split 30
R: my_split 40
#+END_EXAMPLE
--
Nick Anderson
Doer of things, CFEngine