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

Oddly enough, I was just composing a post asking for the same thing (a sort 
function).  Because of the probability of a response which indicated "you 
shouldn't do things that require a specific order in CFEngine; solve the 
problem differently", I was thinking that the workaround would probably use 
this knowledge:

sauer@pyro:~$ perl -le '$,=q{ };print sort @ARGV' 4 1 3 2
1 2 3 4


To do something like this:

sauer@pyro:~/Documents/cfengine_test$ cat ./sort_test.cf
bundle agent sort_test {
vars:
  "a[4]" string => "four";
  "a[1]" string => "one";
  "a[3]" string => "three";
  "a[2]" string => "two";

  "a_nosort"
    slist => getindices( "a" );
  "a_nosort_concat"
    string => join( " ", "a_nosort" );
  "sorter"
    string => "/usr/bin/perl -le '$,=q{ };print sort @ARGV'";
  "a_sort_tmp"
    string => execresult( "$(sorter) $(a_nosort_concat)", "noshell" );
  "a_sorted"
    slist => splitstring( "$(a_sort_tmp)", "\s", "inf" );
  "a_sorted_concat"
    string => join( " ", "a_sorted" );

reports:
  cfengine::
    "unsorted list: $(a_nosort_concat)";
    "sorted list:   $(a_sorted_concat)";
    "sorted element: $(a[$(a_sorted)])";
}
sauer@pyro:~/Documents/cfengine_test$ cf-agent -K -b sort_test -f ./sort_test.cf
R: unsorted list: 4 1 3 2
R: sorted list:   1 2 3 4
R: sorted element: one
R: sorted element: two
R: sorted element: three
R: sorted element: four


It seems from my fairly limited testing that the elements of the array are 
processed by getindices in the order that they're declared in the file; if 
they're declared in order, they look like they pretty much stay in order.

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

Reply via email to