Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you specify range to end of list?

Tags:

groovy

Consider the following statement:

process.text.readLines[3..<-1]

It seems like it should work. Essentially, strip off the first two elements of the array. However, the range operator is confused by the ending -1, since its less than -1. You can easily solve this problem by storing the array as a variable and replacing -1 with size() but that requires an extra line and the definition of a variable. Any other ideas how to express this easily?

like image 237
dromodel Avatar asked Dec 26 '22 13:12

dromodel


2 Answers

I believe you could do:

process.text.readLines()[ 2..-1 ]

or:

process.text.readLines().drop( 2 )
like image 180
tim_yates Avatar answered Jan 03 '23 20:01

tim_yates


This will also do the trick:

process.text.readLines().with { it[2..size()-1] }

It's longer than simply calling drop as suggested above, but it might read a little better depending on the larger context. with lets you get around defining a new variable.

like image 20
cholick Avatar answered Jan 03 '23 20:01

cholick