Forum: CFEngine Help
Subject: array indexes and order.  Do we need sort?
Author: neilhwatson
Link to topic: https://cfengine.com/forum/read.php?3,26932,26932#msg-26932

If an array in Cf is an associative list, then the order of the index is not 
guaranteed. Is this a correct assertion? For example.


$ cat order.cf
body common control {

        bundlesequence => {
                "main",
        };

        inputs => {
                "cfengine_stdlib.cf",
        };
}

bundle agent main {

        vars:
                "dim_items"
                        comment => "Read data file for parsing.",
                        int     => readstringarrayidx(
                                "i",
                                "${sys.workdir}/inputs/order.txt",
                                "\s*#[^\n]*",
                                "\s*;;\s*",
                                "100",
                                "5k"
                                );

                "ii" slist => getindices("i");

        classes:
                "index_ii_defined" expression => isvariable("ii");

        reports:
                index_ii_defined::
                        "${i[${ii}][0]} -> ${i[${ii}][1]}";
}


$ cat order.txt
me ;; item 1
fo ;; item 2
fi ;; item 3
xu ;; item 4
pq ;; item 5

$ cf-agent -If ./order.cf
R: me -> item 1
R: fo -> item 2
R: fi -> item 3
R: xu -> item 4
R: pq -> item 5


In this example the order of the array matched the order of the lines in the 
file.  Is this guaranteed? If not then I feel we need some sort of sort 
function.
 "ii" slist => sort( getindices("i"), "numeric" ); 
Thoughts?

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to