Inedo Community Forums Forums
    • Recent
    • Tags
    • Popular
    • Login

    Welcome to the Inedo Forums! Check out the Forums Guide for help getting started.

    If you are experiencing any issues with the forum software, please visit the Contact Form on our website and let us know!

    Error iterating list of strings

    Scheduled Pinned Locked Moved Support
    buildmaster
    5 Posts 3 Posters 18 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jstarbird_7831
      last edited by

      I have a release template variable that is a list of string values. In my plan I am trying to iterate over it but I am getting a error that it is expecting an enumerable.
      The variable is setup to allow multiple values to be selected and it has the values entered one per line.
      I do use list vars elsewhere but this is the first time I've set one to allow multiple values to be selected and restrict it to the values of the list only.
      In the case of my error I am only selecting one value.

      is there some special handling of this type that has to be done??

      Product: BuildMaster
      Version: 6.0.10

      1 Reply Last reply Reply Quote 0
      • J Offline
        jharbison
        last edited by

        I just recreated this. It appears that when only a single item is selected from the list the variable is created as a scalar(string) rather than a vector(list). This behavior is similar to Powershell's array unrolling so I don't know if this behavior is by design or if it is a bug. I was going to suggest a work around using the optional parameter of $IsVariableDefined to determine if the variable is currently a string or a list but it seems that function does not behave as expected as it returns true for me regardless of the variable's type. It wouldn't be pretty but I guess you could wrap your logic in a try/catch and attempt the logic again but treating the variable as scalar. Something like

        try
        {
            Log-Information The first item is @MyVariable[0];
        }
        catch
        {
            Log-Debug Failure accessing MyVariable. Trying as a scalar.;
            Log-Information The first item is $MyVariable;
        }
        
        1 Reply Last reply Reply Quote 0
        • J Offline
          jstarbird_7831
          last edited by

          I can easily have one value in an array in Powershell so not to sure what you're referring to there.

          $test= @('onevalue')
          
          
          foreach ($val in $test)
          {
              $val
          }
          

          Which will output the one value.

          Anyway, I will try the suggestion for working around this issue.

          1 Reply Last reply Reply Quote 0
          • J Offline
            jstarbird_7831
            last edited by

            I've submitted a bug request for this and I also have a little bit more useful workaround.
            The problem with the Try/Catch is handling real errors if you doing things within the loop of the list.

            The ListCount function still works here even if there is one value so i you have:

            set $listcnt = $ListCount(@listvar);
            
            if $Compare($listcnt,>,1)
            {
                 foreach ($litem in @listvar)
                 {
                     <perform some actions for each @listvar item>
                 }
            }
            else
            {
                 <perform some actions on the $listvar item>
            }
            
            1 Reply Last reply Reply Quote 0
            • T Offline
              thoven
              last edited by

              This is also fixed (BM-3271) starting in v6.1.0

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post
              Inedo Website Home • Support Home • Code of Conduct • Forums Guide • Documentation