Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerate list in groovy

I'm trying to use something like the python enumerate() method in groovy.
E.g.:

list = ['book','pencil','laptop','coffee']
for product, line in enumerate(list, 1):
    print "Product %s in the %sth line" % (product, line)

Output should be:
Product book in the 1th line
Product pencil in the 2th line
Product laptop in the 3th line
Product coffee in the 4th line

Is there a way to do the enumerate method in groovy?
Regards!

like image 787
alex_pc89 Avatar asked Oct 17 '25 17:10

alex_pc89


1 Answers

['book','pencil','laptop','coffee'].eachWithIndex { name, i ->     
    println "Product ${name} in the ${i+1} line" 
}
like image 80
BZ. Avatar answered Oct 19 '25 12:10

BZ.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!