Reputation: 61
In groovy, How to add zero in empty fields of versions in list
def list = [1.0,
1.9,
1.11.0,
1.6,
1.7,
1.7.1,
1.8]
Expected Output
1.0.0,
1.9.0,
1.11.0,
1.6.0,
1.7.0,
1.7.1,
1.8.0
Upvotes: 0
Views: 112
Reputation: 27245
The code you show there is not valid Groovy code and will not compile. You can not define a number like 1.11.0
. That would have to be a String.
The following generates your desired output for that specific data input:
def list = ['1.0',
'1.9',
'1.11.0',
'1.6',
'1.7',
'1.7.1',
'1.8']
println list.collect {
String output = it
if(output.count('.') < 2) output += '.0'
output
}.join(',\n')
Could also do it like this:
def list = ['1.0',
'1.9',
'1.11.0',
'1.6',
'1.7',
'1.7.1',
'1.8']
println list.collect {
if(it.count('.') < 2) it += '.0'
it
}.join(',\n')
Or this:
def list = ['1.0',
'1.9',
'1.11.0',
'1.6',
'1.7',
'1.7.1',
'1.8']
println list.collect {
it.count('.') < 2 ? it += '.0' : it
}.join(',\n')
Upvotes: 1