0

I've got a problem with lists in groovy, I have the following inputs to my script

Script inputs

My requirement is to put my inputs into a list like the following example provided by a user here:

final int OID = 0
final int TASK = 1
final int START = 2
final int END = 3
final int REALSTART = 4
final int REALEND = 5

List<Object[]> input = [
        [ 'oid', 'task', 10, 20, 11, 25 ],
        [ 'oid2', 'task2', 25, 50, null, null ]
]


List<List> output = [ ]

input.each { row ->
    output << [ row[ OID ], row[ TASK ], row[ START ], row[ END ] ]
    if ( row[ REALSTART ] && row[ REALEND ] ) {
        output << [ row[ OID ], row[ TASK ] + '_Real', row[ REALSTART ], row[ REALEND ] ]
    }
}

My problem is the input list part, I can't figure how to fill it like the example so that my input list result is:

[oid1,task1,start1,end1,realstart1,realend1]
[oid2,task2,start2,end2,realstart2,realend2]
[oid3,task3,start3,end3,realstart3,realend3]
[oid4,task4,start4,end4,realstart4,realend4]
[oid5,task5,start5,end5,realstart5,realend5]

with my given input values. timeNow is irrelevant in this case.

Is it clear enough?

3
  • 2
    "Is it clear enough?" Not really, no. What do you have (example input), what do you require (example output), what have you tried, and what problems have you hit? Commented Apr 18, 2017 at 8:36
  • ok you want to restructure your input into [oid1,task1,start1,end1,realstart1,realend1] format but what does it look like intially? Commented Apr 18, 2017 at 8:39
  • initially is like: List<int> oid, List<String> task, List<Timestamp> start and so on. So it's a list for each attribute. Imagine those are arrays, i need to have my output like: { oid[0],task[0],start[0]...}{ oid[1],task[1],start[1]..} .. Commented Apr 18, 2017 at 8:51

2 Answers 2

2

To combine each nth element of a list of lists, groovy has the transpose function:

[["oid1","oid2","oid3","oid4","oid5"],
 ["task1","task2","task3","task4","task5"],
 ["start1","start2","start3","start4","start5"],
 ["end1","end2","end3","end4","end5"],
 ["realstart11","realstart12","realstart13","realstart14","realstart15"],
 ["realend11","realend12","realend13","realend14","realend15"],].transpose()

// [[oid1, task1, start1, end1, realstart11, realend11], [oid2, task2, start2, end2, realstart12, realend12], [oid3, task3, start3, end3, realstart13, realend13], [oid4, task4, start4, end4, realstart14, realend14], [oid5, task5, start5, end5, realstart15, realend15]]
Sign up to request clarification or add additional context in comments.

1 Comment

interesting did not know that method :)
1

Try it like this.
Input (per your comment):

def oids = ["oid1","oid2","oid3","oid4","oid5"];
def tasks = ["task1","task2","task3","task4","task5"];
def starts = ["start1","start2","start3","start4","start5"];
def ends = ["end1","end2","end3","end4","end5"];
def realstart1s = ["realstart11","realstart12","realstart13","realstart14","realstart15"];
def realend1s = ["realend11","realend12","realend13","realend14","realend15"];

Algorithm:

def result = [];

oids.size().times{
    result << [oids[it], tasks[it], starts[it], ends[it], realstart1s[it], realend1s[it]]
}

Output:

[
[oid1, task1, start1, end1, realstart11, realend11],
[oid2, task2, start2, end2, realstart12, realend12],
[oid3, task3, start3, end3, realstart13, realend13], 
[oid4, task4, start4, end4, realstart14, realend14], 
[oid5, task5, start5, end5, realstart15, realend15]
]

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.