Home > Net >  reconf map 3 level in Groovy 2.4.7
reconf map 3 level in Groovy 2.4.7

Time:07-26

Need to convert map in Groovy 2.4.7:

[
  1:[
    Par1:[
      Podpar1:[s:str1, time:16],
      Podpar2:[s:str2, time:16]
    ],
    Par2:[
      Podpar1:[s:0, time:16],
      Podpar2:[s:0, time:16]
    ]
  ],
  2:[
    Par1:[
      Podpar1:[s:str3, time:16],
      Podpar2:[s:str4, time:16]
    ],
    Par2:[
      Podpar1:[s:0, time:16],
      Podpar2:[s:0, time:16]
    ]
  ]
]

to:

[
  1:[
    Podpar1:[
      Par1:[s:str1, time:16],
      Par2:[s:0, time:16]
    ],
    Podpar2:[
      Par1:[s:str2, time:16],
      Par2:[s:0, time:16]
    ]
  ],
  2:[
    Podpar1:[
      Par1:[s:str3, time:16],
      Par2:[s:0, time:16]
    ],
    Podpar2:[
      Par1:[s:str4, time:16],
      Par2:[s:0, time:16]
    ]
  ]
]

Code which i try to use is:

def res =  [ "1" : [ "Par1" : [ "Podpar1" : [ "s" : "str" , "time" : 16 ] , "Podpar2" : [ "s" : "str" , "time" : 16 ]],
                          "Par2" : ["Podpar1" : ["s" : 0 ,"time" : 16] , "Podpar2" : ["s" : 0 , "time" : 16 ]],
                  "2" : ["Par1" : ["Podpar1" : ["s" : "str1" ,"time" : 16] ,"Podpar2" : ["s" : "str1" ,"time" : 16]]],
                         "Par2" : ["Podpar1" : ["s" : 0 ,"time" : 16 ] ,"Podpar2" : ["s" : 0 ,"time" : 16]]]]

def reconfMap = [:]
res.each { k, v ->
    reconfMap[k] = [:]
    def temppar = [:]
    def temppodpar = [:]
    v.each { park, parv ->
        parv.each { podpark, podparv ->
            temppar[park]=podparv
            temppodpar[podpark]= temppar
            reconfMap[k] = temppodpar
        }
    }
}

but all elements of map are replacing by last element. Can anyone help me?

Given result:

[1:[Podpar1:[Par1:[s:str2, time:16], Par2:[s:0, time:16]], Podpar2:[Par1:[s:str2, time:16], Par2:[s:0, time:16]]], 2:[Podpar1:[Par1:[s:str4, time:16], Par2:[s:0, time:16]], Podpar2:[Par1:[s:str4, time:16], Par2:[s:0, time:16]]]]

Expected result:

[1:[Podpar1:[Par1:[s:str1, time:16], Par2:[s:0, time:16]], Podpar2:[Par1:[s:str2, time:16], Par2:[s:0, time:16]]], 2:[Podpar1:[Par1:[s:str3, time:16], Par2:[s:0, time:16]], Podpar2:[Par1:[s:str4, time:16], Par2:[s:0, time:16]]]]

CodePudding user response:

What a confusing example

  • Related