File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Groovy and the fly likes This is probably obvious Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "This is probably obvious" Watch "This is probably obvious" New topic

This is probably obvious

Bill Dowd

Joined: Aug 17, 2012
Posts: 4
It's driving me insane! I'm pretty new to groovy and programming in general, and I'm writing a simulation model using RePast, which is based on Groovy. I created a map 'schedmap' which has other maps' names as its values:

I also created an empty map called 'schedmaps1'. What follows is troubleshooting code:

I get the following output for lines 2-5 above:

So, for lines 2 and 3, I get the map printed, which is what I want. The problem is that I can't explicitly state that I want schedmaps1, because the map I need will vary from iteration to iteration, so I need to call the correct map from schedmap, based on another object's variables (line 4) or at least allow the spcID (from another class) to dictate the correct map (a la line 5). The funny thing is, I was able to call a map from a map in other code, but I am completely unable to get it to work now. I guess the ultimate question is, in lines 4 and 5, how do I get Groovy to see 'schedmaps1' as a reference instead of as final output?

Many thanks in advance!
Bill Dowd

Joined: Aug 17, 2012
Posts: 4
Hmm, it looks like I got it. If I get the current object and call the maps with respect to that object (i.e., scheduler.schedmaps1) it works. That doesn't make sense, but I'll run with it!
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

A map of maps to values is usually a "smell" that indicates you're trying too hard to write flexible code while sacrificing clarity of purpose. Your short description alone takes a couple of re-readings to just begin to follow. I bet if you read the code you wrote a few weeks from now, you'd be scratching your head. If not two weeks, then two months from now. The point is, the logic is convoluted and that's never a good thing.

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: This is probably obvious
It's not a secret anymore!