in reply to Simplify HoH code
Note that given/when and smart matching is considered experimental, and might change in future releases of Perl (this is in fact currently being discussed). In this case, your conditions are so simple that they could be replaced by a simple if/elsif. That's also how I would suggest solving your question about matching multiple variables at once, e.g. if ($month eq 'FEB' && $week eq 'W1').
Also, one thing to "simplify" the code would be to use consistent formatting (like where you put the braces) and indentation. perltidy could help you with this.
Lastly, is this code really representative of your actual code? The reason I ask is that you have variables named like $FEB_L1_W1. I see two possibilities: Either you have a fixed set of variables for specific months that won't change. In that case, you don't need to loop over the keys of %Hash to find those months - just say $Hash{FEB} and $Hash{APR} to access those.
Or, you are planning your actual code to have a bunch more variables named $MONTH_L1_W1 etc. In that case, you shouldn't store them as individual variables, but instead be putting those in a hash as well, in which case you can get rid of the repetitive code in the loop and loop over both hashes. We could probably show an example of this, but at the moment I don't quite understand what your code is doing (probably because you haven't explained the magic numbers in your code).
If you could explain more about what your code is supposed to be doing, and show an SSCCE that is perhaps more representative, and some more sample input and the expected output, then we could probably help more and suggest better solutions.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Simplify HoH code
by greetsathya (Initiate) on Dec 03, 2017 at 11:11 UTC | |
by haukex (Archbishop) on Dec 03, 2017 at 11:51 UTC | |
by greetsathya (Initiate) on Dec 03, 2017 at 12:37 UTC |