Search code examples
scopingibm-doors

Scoping problem when trying to assign vector of strings in DOORS DXL


I want to be able to send a selector value to a function, and have the function create a string array of values for use in subsequent operations. The problem I'm running into is that the DXL rules don't "see" a declared variable inside an "if {} " block, even when I've guaranteed that in all cases my string array would get initialized. Example:

string  tryme( int thechoice){
    string outit
    if (thechoice == 1){
         outit = "you chose one"
        }
    else if (thechoice ==2){
         outit = "you chose two"
        }
        else { outit = "bad choice"}
//  do a bunch of stuff with  "outit" values 
    return outit
}

// that works, but this doesn't
string  trymore( int thechoice){
    if (thechoice == 1){
        string outit[] = {"you chose one","and one"}
        }
    else if (thechoice ==2){
        string outit[] = {"you chose two","and two", "and three"}
        }
        else { string outit = "bad choice"}
//  do a bunch of stuff with  "outit" values 
    return outit
}

I could use dynamic arrays, thus allowing me to declare the array prior to the "if{}", but then I'm forced to write loops inside each case, using put to load the array.
I know DXL has a limited capability, but if anyone knows a better approach please let me know.

additional constraint

In the end, I want to be able to call the function repeatedly from a loop, changing "thechoice" each time. That is why I can't declare the string array in the parent script, because once declared ( string outit[] = {'a','b'} ) , DXL cannot delete or resize the array.


Solution

  • It's easier than that, tho' a bit counterintuitive . I need to declare a string vector with out assigning anything, then generate a temporary string vector, then set my desired variable equal to the temp. Like this:

    string  trymore( int thechoice){
        string outit[]
        string whatdone
        if (thechoice == 1){
            string foo[] = {"you chose one","and one"}
            whatdone = "did one"
                outit = foo
            }
        else if (thechoice ==2){
            string foo[] = {"you chose two","and two", "and three"}
            whatdone = "did two"
                outit = foo
            }
            else {
            string foo[] = "bad choice"
            whatdone = "nogood"
                outit = foo
                }
    print "outit " outit[0] "\n"
    //  do a bunch of stuff with  "outit" values 
        return whatdone
    }
    

    DXL will not allow you to assign the values to outit , crying "length mismatch", but will allow you to set outit equal to the fully defined foo