Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-2720

As a policy writer I would like to be able to easily get the index of a list

    XMLWordPrintable

    Details

    • Type: Story
    • Status: Open
    • Priority: Lowest
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
    • Complexity:
      S

      Description

      Sometimes it's necessary to use nth() on a list to access an element in a specific position.

      Currently in order to get a list of the element positions in a list you need to first get the length() of the list and then you must subtract 1 since length() counts from 1 but positions are 0 based while being careful to format the floating point number returned by eval() as an integer . Finally the list of all index positions can be generated using expandrange(). That's 3 promises and 4 functions to get the index of a list.

      For example:

      bundle agent main
      {
      
        vars:
            "list" slist => { "zero", "one", "two", "three" };
      
            "list_length" int => length( list );
      
            "last_index_position"
              string => format( "%d", eval( "$(list_length)-1", "math", "infix" ));
      
            "index" slist => expandrange( "[0-$(last_index_position)]", 1 );
      
      
        reports:
            "index $(index) = $(with)"
              with => nth( list, $(index) );
      
      }
      

      Produces:

      R: index 0 = zero
      R: index 1 = one
      R: index 2 = two
      R: index 3 = three
      

      It would be convenient if I could simply get the index of a list directly.

      For example:

      bundle agent main
      {
      
        vars:
            "list" slist => { "zero", "one", "two", "three" };
            "i" slist => getindices( list );
      
      
        reports:
            "index $(i) = $(with)"
              with => nth( list, $(i) );
      
      }
      

      Would produce:

      R: index 0 = zero
      R: index 1 = one
      R: index 2 = two
      R: index 3 = three
      

      However currently getindices() on a list returns an empty list.

      R: length of index produced by getindices = 0
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                a10003 Eystein Maloy Stenberg
                Reporter:
                a10042 Nick Anderson
              • Votes:
                1 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Summary Panel