0

I'm having some trouble figuring out how to accomplish what I am looking for. Let's say I have 5 different String variables: String string1 = "A"

String string2 = "B"

String string3 = "A"

String string4 = "B"

String string5 = "A"

What I want to do is loop over those 5 variables and if they equal B, I want to set them to A. What I have tried is something like:

[string1,string2,string3,string4,string5].each {
     if (it == "B"){
        //don't know what to put here
        need to set variable = "A" here
     }
}

I don't know how to get the actual variable name in the closure for the assignment. Maybe I am not searching correctly for what I am trying to do, but I need to be able to set strin2 and string4 to "A" with this loop.

I know I can do 5 if statements, but I know there has got to be a better way.

3
  • Do you want to change the object that variables like string1 point to, or do you want to leave that reference as it is and just put a new value in the List in place of the old one (which would not have any impact on the string1 reference)? Commented Dec 15, 2020 at 20:59
  • I want to change what it points to, from A to B. I don't care what the original value was. Commented Dec 15, 2020 at 21:01
  • @jcattau, it depends how you declared those string1-string5. if those are the members/properties of some object/map/script - then obj['string1'] = "B" will do the assignment. if they are the local variables - then no chance. Commented Dec 15, 2020 at 21:42

1 Answer 1

1

you can set an object (class) member with obj[property] = value if it's a groovy object or map

for example

@groovy.transform.ToString
class X {
    String string1 = "A"
    String string2 = "B"
    String string3 = "A"
    String string4 = "B"
    String string5 = "A"

    void switchAll(vOld,vNew){
        this.getProperties().each{k,v->
            if(k ==~ /string\d+/ && v==vOld) this[k] = vNew
        }
     }
     
}

def x = new X()
println x

x.switchAll("B","C")
println x

prints:

X(A, B, A, B, A)
X(A, C, A, C, A)
Sign up to request clarification or add additional context in comments.

3 Comments

I'm going to see if I can work with this, but the strings1 through 5 may not always be "A" or "B", so I want to loop through them and check if they are "A" and if so change to "B". But these are local variables I was trying to work with.
there is a condition in my answer if( ... v==vOld). and you mentioned in your question that it's a class variables. with local variables - nothing will work.
I'm sorry, I know enough about development for automated testing but still struggle with some concepts. They are class variables declared in the class, not local variables. I mixed up the concept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.