user7083100 user7083100 - 22 days ago 9
Groovy Question

Groovy pop min from array

I need to find the minimum value of an array then remove it.

How can I use

min()
and
pop()
in one statement ?

So I have five files, number (1,2,3,4,5) in a loop.
After processing the first file, I need to remove it.

When I do pop alone, it picks up file number 5 and drops it and then process next file 4, and so on.

I need to reverse the order which the files are being processed:

def fileName = fileList.pop()
def testCaseName = fileName[0..-5]

Answer

Instead of pop() the list elements if you want to loop the list in order why you don't use each to iterate the list in desired order:

fileList.each{ fileName ->
     def testCaseName = fileName[0..-5]
     ...
}

Or if you can not use each and you need to have the remaining elements in a list you can use reverse() before start pop()ing elements:

fileList = fileList.reverse()
def fileName = fileList.pop()
def testCaseName = fileName[0..-5]
...
Comments