views:

23

answers:

1

Hi,

This is OK

def variables=[
              ['var1':'test1'],
              ['var2':'test2'],
              ['var3':'test3']
              ]

println "${variables.size()}" 
variables.each{entry ->   
  println "${entry} "   
}

I got:

3
[var1:test1] 
[var2:test2] 
[var3:test3] 

but this caused problems

def variables=[
                  ['var1':'test1'],
                  ['var2':'test2'],
                  ['var3':'test3']
                  ]

    println "${variables.size()}" 
    variables.each{entry ->   
      println "${entry.key} "   
    }

since I got:

3
null 
null 
null 

I'm expecting:

3
var1
var2
var3 

what's wrong with my code?

thank you!!!

A: 

You want:

def variables=[
    'var1':'test1',
    'var2':'test2',
    'var3':'test3'
]

println variables.size()
variables.each{entry ->   
    println entry.key
}

Before you had an ArrayList containing three LinkedHashMap objects. The above code is a single LinkedHashMap with three entries. You also don't need string interpolation, so I removed it.

Matthew Flaschen