It might sound simple, but am kind of struglling with the logic of how to print all but ignor the last element in the list. Any suggestion would be helpful.
def list = [
'homePage',
'productPage',
'basketPage',
'categoryPage'
]
def counter = 1
list.each { element ->
if (element == list.last()){
list.remove(3)
println "Item $counter -" + element
}
}
Item 1 - homePage
Item 2 - productPage
Item 3 - basketPage
It can be shorter than @SteveD answer:
println list[0..-2]
Groovy supports range operations on collections:
print list[0..list.size-2]
This will print:
[homePage, productPage, basketPage]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With