Question

I have the following groovy-script:

#!/usr/bin/env groovy 
def files = [ 
    'file-1.bat' :  
        [       
            'content-1-1', 
            'content-1-2', 
            'content-1-3' 
        ],      
    'file-1' : 
        [       
            'content-unix-1-1', 
            'content-unix-1-2', 
            'content-unix-1-3', 
        ],      

    'file-2.bat' :  
        [       
            'content-2-1', 
            'content-2-2', 
            'content-2-3' 
        ],      
    'file-2' : 
        [       
            'content-unix-2-1', 
            'content-unix-2-2', 
            'content-unix-2-3', 
        ],      
] 

files.each { 
    file_key, file_value -> println file_key; 
    file_value.each { 
        file_content -> println "\t"+file_content; 
        files[ file_key ] = [file_content : true]; 
    }   
} 

println ' Upgraded content'; 

files.each { 
    file_key, file_value -> println file_key; 
    file_value.each { 
        file_content -> println "\t"+file_content; 
    }   
} 

The following lines causes my problems:

files[ file_key ] = [file_content : true]; 

What I want to do is to create for every entry in the map also the true part of the key:value pair...I know that I didn't define the list in that way... I've tried to enhance the map by using files[file_key].put(key, value); but this does not work...Maybe I'm thinking in a complete wrong direction... The background of that construct is that in the files (file-1.bat, file-1 etc.) I will check the existence of the contents given as a Map

'file-1.bat' : [       
     'content-1-1', 
     'content-1-2', 
     'content-1-3' 
 ],

I could do the following:

'file-1.bat' : [       
     'content-1-1' : false, 
     'content-1-2' : false, 
     'content-1-3' : false, 
 ],

but that's exactly what I want to avoid.

Was it helpful?

Solution

You are right in thinking that put() will solve your issue, but you cannot put a map element into a list. You need to first create a map that can be put into, then assign the resulting map as the output.

For example:

files.each { 
    file_key, file_value -> println file_key; 
    def file_map = [:]; 
    file_value.each { 
        file_content -> println "\t"+file_content; 
        file_map.put(file_content, true); 
    }
    files[ file_key ] = file_map; 
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top