monda
monda

Reputation: 3915

Groovy: Add some element of list to another list

list1 = [[1,'Rob','Ben', 'Ni', 'cool'],[2,'Jack','Jo','Raj','Giri'],[...]....]

list2 = [['20 May 2013',20],['25 May 2013',26],[...]....]

there will be 100 of such records

i want the resulting list like

list1 = [[1, '20 May 2013', 20, 'Rob','Ben','Ni', 'cool'],[2,'25 May 2013', 26, 'Jack','Jo','Raj','Giri']]

any suggestion ?

Upvotes: 0

Views: 1249

Answers (3)

dmahapatro
dmahapatro

Reputation: 50245

[list1, list2].transpose()*.flatten()

Assuming cardinality of list1 and list2 is same.

UPDATE
Question is modified drastically now, but you can get what you seek by extending the transpose as below:

[list1, list2].transpose()*.flatten()
                           .collect{[it[0], it[-2..-1], it[1..-3]]}*.flatten()

Upvotes: 3

user2418182
user2418182

Reputation:

The result row isn't the concatenation of the two list rows - it takes the second row and inserts it into index 1 of the first row, without the last two elements. So

[list1, list2].transpose()*.flatten()

won't work - tho' it is pretty cool :-). However,

[list1,list2].transpose().collect { def l = it.flatten(); l[0..0] + l[5..6] + l[1..2] }

gives the result show in the question. And I fully admit to standing on the backs of giants :-)

Upvotes: 0

bdkosher
bdkosher

Reputation: 5883

How about this?

def i = 0
def combined = list1.collect([]) { it + list2[i++] }

Upvotes: 0

Related Questions