12

I want to assign the array item into variable directly using groovy like this:

def str = "xyz=abc"
def [name, value] = str.split("=")

but groovy doesn't like it. Is there a way to do that (not storing the array result and get the index[0], index[1] from it?).

Thanks,

2 Answers 2

27

You just need parenthesis instead of brackets:

def str = "xyz=abc"
def (name, value) = str.split("=")

enter image description here

Note that you'll need to know how many elements you're expecting or you'll have unexpected results.

Sign up to request clarification or add additional context in comments.

2 Comments

what is the different between [] and ()? Sorry for newbie question.
while brackets are appropriate for arrays, the parenthesis are how Groovy does multiple assignment. See groovy.codehaus.org/Multiple+Assignment
5
def name, value
(name,value) = str.split("=")

You just need to do your definition before your multiple assignment.

Comments

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.