• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Closure does not iterate over values properly, only returns the first value.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am relatviely new to Groovy and the concept of closures. I am working on a report which displays query results in a table structure.
I am using a closure to iterate over the row values in order to return the results, but it only returns results for the first days value, leaving the other values blank, which I know to contain data from looking at the database. The code which does this is as follows:



To explain the code, I am passing the start and end dates and the report metric name as variables, which all work.
These are passed into the above query. I am returning the report date and values from this query. Then I am using the closure to iterate over each row and put a title of "Day1" etc. + the values from those rows into the map. From here the map is added to a table to dispaly. The table only return values for the 1st day, leaving the rest blank. So it appears the closure is not iterating the row.values properly.
Any help with this would be very much appreciated.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I'm wrong, but shouldn't the variable i be declared outside the closure so that it doesn't get initialized to 1 for each row?
 
Ann Doyle
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
I have tried initializing the variable i outside of the closure, but that makes no difference to the result, it is able to iterate over the Days, for example Day 1, Day 2, Day3 are displaying.
I have also noticed that I am getting a warning for "Cannot infer argument types" on the line:
mapToDisplay.put(day, row.value)

However, the code runs fine, so I'm not sure if that is part of the problem, as I have looked it up and it is common to intellij idea, but usually means nothing.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line is the problem:



 
Ranch Hand
Posts: 49
1
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try using eachWithIndex.

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John's solution is the most elegant and easiest. But to demonstrate what you did wrong. Here is where we assign the var outside the loop and the results. Run this code in a Groovy Console and look at the results.

Basically, I took out the query from the equation as you will see why stuff isn't getting into the Map. Then after you fix your code, put a break point or printout the values in your map so you can see the values inside there, then if it doesn't show up in your table then it is the table/view that is wrong.



Mark
 
Ann Doyle
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the feedback everyone! Mark, I did as you said and tested this in groovyConsole, then went ahead and implemented it in my own code and it works!

I now see the problem with assigning i inside my closure. I still have a lot to learn about closures to be honest, but these answers we're really helpful. Thank you!
 
Ann Doyle
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So by running this in groovyConsole I could see that by assigning int i = 1++ inside the closure I got the following output:
day: Day1
row: [123, Hello]
day: Day1
row: [345, World]
[Day1:[345, World]]
So the map was only going to contain the one key, value pair as it kept iterating over the value of Day1 instead of all of the values.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic