Looping over nested maps

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Looping over nested maps

mantawolf
Hi.  new to groovy and need some help understanding how to loop over nested maps.  I keep running into issues and I am still learning this.

So roughly, I have a nested map like below. I need to loop over the Dallas/Austin level then loop over the s6### level and lastly over the last level to output the data.  Can someone provide some insight for a newb on how to accomplish this?  I need to loop over it dynamically with "map { key, row ->" because I need to output the key names too.

[
        Dallas:
                [s6564:
                        [
                                [AreaName:Test, Ord_No:172517, Store_no:s6564, Ord_Dt:2011-02-09],
                                [AreaName:Test, Ord_No:172583, Store_no:s6564, Ord_Dt:2011-02-09],
                                [AreaName:Test, Ord_No:172630, Store_no:s6564, Ord_Dt:2011-02-09]
                        ]
                ],
        Austin:
                [s6805:
                        [
                                [AreaName:Loehr, Ord_No:281842,Store_no:s6805, Ord_Dt:2011-02-09 00:00:00.0]
                        ]
                ]
]
Reply | Threaded
Open this post in threaded view
|

Re: Looping over nested maps

David Clark
def theMap =
  [ Dallas:
    [s6564:
     [
      [AreaName:'Test', Ord_No:172517, Store_no:'s6564', Ord_Dt:'2011-02-09'],
      [AreaName:'Test', Ord_No:172583, Store_no:'s6564', Ord_Dt:'2011-02-09'],
      [AreaName:'Test', Ord_No:172630, Store_no:'s6564', Ord_Dt:'2011-02-09']
      ]
     ],
    Austin:
    [s6805:
     [
      [AreaName:'Loehr', Ord_No:281842,Store_no:'s6805', Ord_Dt:'2011-02-09 00:00:00.0']
      ]
     ]
    ];

theMap.each { cityKey, cityRow ->
  println("City: ${cityKey}");
  cityRow.each { codeKey, codeList ->
    println("Code: ${codeKey}");
    codeList.each { map ->
      map.each { key, value ->
        println("Key: ${key}, Value: ${value}"); }; }; }; };

On Sunday, April 17, 2011, you wrote:

> Hi.  new to groovy and need some help understanding how to loop over nested
> maps.  I keep running into issues and I am still learning this.
>
> So roughly, I have a nested map like below. I need to loop over the
> Dallas/Austin level then loop over the s6### level and lastly over the last
> level to output the data.  Can someone provide some insight for a newb on
> how to accomplish this?  I need to loop over it dynamically with "map { key,
> row ->" because I need to output the key names too.
>
> [
> Dallas:
> [s6564:
> [
> [AreaName:Test, Ord_No:172517, Store_no:s6564, Ord_Dt:2011-02-09],
> [AreaName:Test, Ord_No:172583, Store_no:s6564, Ord_Dt:2011-02-09],
> [AreaName:Test, Ord_No:172630, Store_no:s6564, Ord_Dt:2011-02-09]
> ]
> ],
> Austin:
> [s6805:
> [
> [AreaName:Loehr, Ord_No:281842,Store_no:s6805, Ord_Dt:2011-02-09
> 00:00:00.0]
> ]
> ]
> ]
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Looping-over-nested-maps-tp4309737p4309737.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>


--
David

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Looping over nested maps

mantawolf
Holy crap was I ever over-complicating that.  Thanks for the response David.  Kept trying to figure our how I would do it in other languages and couldn't find a reference on how to do it in Groovy.

map[var][var2] really doesn't work here...  

Reply | Threaded
Open this post in threaded view
|

Re: Looping over nested maps

David Clark
You can chain calls to the [] operator in groovy.  Thus something like map[var1][var2] is legal and I use it all the time.  The problem is that with deeply nested structures of different types the [][] syntax can be more confusing than helpful.

On Sunday, April 17, 2011, you wrote:

> Holy crap was I ever over-complicating that.  Thanks for the response David.
> Kept trying to figure our how I would do it in other languages and couldn't
> find a reference on how to do it in Groovy.
>
> map[var][var2] really doesn't work here...  
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Looping-over-nested-maps-tp4309737p4309789.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>


--
David

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Looping over nested maps

Ed Clark-4
One thing I noticed with your data structure was it was a mix of maps
and lists.
But, as David points out, chaining []s still works.

Try this in the Groovy console:

def theMap =
   [ Dallas:
     [s6564:
      [
       [AreaName:'Test', Ord_No:172517, Store_no:'s6564',
Ord_Dt:'2011-02-09'],
       [AreaName:'Test', Ord_No:172583, Store_no:'s6564',
Ord_Dt:'2011-02-09'],
       [AreaName:'Test', Ord_No:172630, Store_no:'s6564',
Ord_Dt:'2011-02-09']
       ]
      ],
     Austin:
     [s6805:
      [
       [AreaName:'Loehr', Ord_No:281842,Store_no:'s6805',
Ord_Dt:'2011-02-09 00:00:00.0']
       ]
      ]
     ]


println theMap['Austin']['s6805'][0]['Ord_No']

city = 'Dallas'        // map key
store = 's6564'        // map key
orderIdx = 2           // list index
infoKey = 'AreaName'   // map key
println theMap[city][store][orderIdx][infoKey]



Ed


On 04/18/2011 08:48 AM, David Clark wrote:

> You can chain calls to the [] operator in groovy.  Thus something like map[var1][var2] is legal and I use it all the time.  The problem is that with deeply nested structures of different types the [][] syntax can be more confusing than helpful.
>
> On Sunday, April 17, 2011, you wrote:
>> Holy crap was I ever over-complicating that.  Thanks for the response David.
>> Kept trying to figure our how I would do it in other languages and couldn't
>> find a reference on how to do it in Groovy.
>>
>> map[var][var2] really doesn't work here...
>>
>>
>>
>> --
>> View this message in context: http://groovy.329449.n5.nabble.com/Looping-over-nested-maps-tp4309737p4309789.html
>> Sent from the groovy - user mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>      http://xircles.codehaus.org/manage_email
>>
>>
>>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Looping over nested maps

Guillaume Laforge-4

On Mon, Apr 18, 2011 at 16:49, Ed Clark <[hidden email]> wrote:
[...]
println theMap['Austin']['s6805'][0]['Ord_No']

For a specific path like that one, you can also do:

println theMap.Austin.s6805[0].Ord_No
 
And for variable path elements, the one you indicated is fine:

println theMap[city][store][orderIdx][infoKey]


--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one