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



  • 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



  • 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;
    }


  • 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.



  • 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>
    }


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


Log in to reply
 

Inedo Website HomeSupport HomeCode of ConductForums GuideDocumentation