• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Sheriff
Posts: 22948
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4411
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line is the problem:



 
Greenhorn
Posts: 28
Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try using eachWithIndex.

 
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!