Forum: Cfengine Help Subject: Re: How to extract values (not keys) from an array to slist? Author: yrouxel Link to topic: https://cfengine.com/forum/read.php?3,21174,21211#msg-21211
Hi Mike, I had a problem related to yours, but not quite the same. Sometimes, we want to build a new list from an existing one by doing a transformation upon those values. In functionnal programing this is the 'map' operator; in pseudo-code: {"file1", "file2", "file3" } map (x -> '/etc/httpd/'+x) == {"/etc/httpd/file1", "/etc/httpd/file2", "/etc/httpd/file3" } With CFEngine, this could be: "myList1" slist => {"file1", "file2", "file3" } "myList2" slist => {"/etc/httpd/$(myList1)"} Unfortunately, this does not work because, the iteration over 'myList1' is done over the whole '"myList2" slist =>...' declaration; as a consequence, each iteration is flagged by CFEgine as a redefinition of the variable 'myList1'. I did not manage to find a function or a way for CFEngine to provide the equivalent of the 'map' operator. I think this is something that is missing in the toolbox of CFEngine to manipulate data. There are the functions {readstringlist, splitstring} which produce list, but 'readstringlist' reads from a file and 'splitstring' is cumbersome to use. Eventually, I found the function 'getindices' the best candidate to emulate the 'map' operator, though in a contrived way. Rather than trying to explaining, I apply the technique to the above example: "myList1" slist => {"file1", "file2", "file3" } "myList2_array["/etc/httpd/$(myList1)"] => "" "myList2" slist => getindices("myList2_array") With this unconventionnal use of the 'getindices' function, we can mimic the 'map' operator. For our purpose, only the indices are of interest, the content of the array 'myList2_array' is not important, so we use the empty string. The real mapping is carried out in the index déclaration '' It can be as sophisticated as the CFEngine syntax allows for the definition of variables. Regards Yann Rouxel _______________________________________________ Help-cfengine mailing list Help-cfengine@cfengine.org https://cfengine.org/mailman/listinfo/help-cfengine