1#  Copyright 2021 Northern.tech AS
2
3#  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
4
5#  This program is free software; you can redistribute it and/or modify it
6#  under the terms of the GNU General Public License as published by the
7#  Free Software Foundation; version 3.
8
9#  This program is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12#  GNU General Public License for more details.
13
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
17
18# To the extent this program is licensed as part of the Enterprise
19# versions of Cfengine, the applicable Commercial Open Source License
20# (COSL) may apply to this file if you as a licensee so wish it. See
21# included file COSL.txt.
22
23#+begin_src cfengine3
24body common control
25{
26      bundlesequence => { "test" };
27}
28
29bundle agent test
30{
31  vars:
32      "test" slist => {
33                        1,2,3,
34                        "one", "two", "three",
35                        "long string",
36                        "four", "fix", "six",
37                        "one", "two", "three",
38      };
39      "test_str" string => format("%S", test);
40
41      "test2" data => parsejson("[1, 2, 3, null]");
42      "test2_str" string => format("%S", test2);
43
44      "test3" data => parsejson('{ "x": true, "y": "z" }');
45      "test3_str" string => format("%S", test3);
46
47      "nth" slist => { 1, 2, 6, 10, 11, 1000 };
48      "nth2" slist => getindices(test2);
49      "nth3" slist => getindices(test3);
50
51      "access[$(nth)]" string => nth(test, $(nth));
52      "access[0]" string => nth(test, 0);
53
54      "access2[$(nth2)]" string => nth(test2, $(nth2));
55      "access3[$(nth3)]" string => nth(test3, $(nth3));
56
57      "nth_neg1" string => nth(test, "-1");
58      "nth_neg100" string => nth(test, "-100"); # invalid index position requested
59
60  reports:
61      "The test list is $(test_str)";
62      "element #$(nth) of the test list: $(access[$(nth)])";
63      "element #0 of the test list: $(access[0])";
64
65      "The test2 data container is $(test2_str)";
66      "element #$(nth2) of the test2 data container: $(access2[$(nth2)])";
67
68      "The test3 data container is $(test3_str)";
69      "element #$(nth3) of the test3 data container: $(access3[$(nth3)])";
70
71      "The last element of test is $(nth_neg1)";
72      "nth_neg100 is not defined, because an invalid index was requested"
73        if => not( isvariable( nth_neg100 ));
74}
75#+end_src
76###############################################################################
77#+begin_src example_output
78#@ ```
79#@ R: The test list is { "1", "2", "3", "one", "two", "three", "long string", "four", "fix", "six", "one", "two", "three" }
80#@ R: element #1 of the test list: 2
81#@ R: element #2 of the test list: 3
82#@ R: element #6 of the test list: long string
83#@ R: element #10 of the test list: one
84#@ R: element #11 of the test list: two
85#@ R: element #0 of the test list: 1
86#@ R: The test2 data container is [1,2,3,null]
87#@ R: element #0 of the test2 data container: 1
88#@ R: element #1 of the test2 data container: 2
89#@ R: element #2 of the test2 data container: 3
90#@ R: element #3 of the test2 data container: null
91#@ R: The test3 data container is {"x":true,"y":"z"}
92#@ R: element #x of the test3 data container: true
93#@ R: element #y of the test3 data container: z
94#@ R: The last element of test is three
95#@ R: nth_neg100 is not defined, because an invalid index was requested
96#@ ```
97#+end_src
98