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.


In reply to Re: Simplify HoH code by haukex
in thread Simplify HoH code by greetsathya

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.