Ant's
Ant's

Reputation: 13811

How do i replace only required field in Groovy

I have two lists :

a = [1,2,3]
b = ["?",1,2,"?",4,"?"]

In the second list, I need to replace the first "?" with first element of a(i.e a[0]) and second "?" with a[1] and so on(provided that the number of "?" = size of a) and the result as modified b.

How I can do this groovy-er way?

Thanks in advance.

Upvotes: 1

Views: 125

Answers (1)

epidemian
epidemian

Reputation: 19219

Some simple solutions:

This returns the result in a new list (you can assign this result to the b variable)

def i = 0
b.collect { it == "?" ? a[i++] : it }

This modifies the list referenced by b

a.each { b[b.indexOf("?")] = it } 

Upvotes: 4

Related Questions