Hi,

Is it possible to check two variables in given/when statement in below code? ( ie something like given $month , $week ) How to simplify the nested if conditions in below code ? Just thinking to have one subroutine to have few parameters from second for loop to avoid nested ifs & elsifs or any other suggestion is appreciated

update: How to get exit codes for different checks ie month/week base and print successful value , failure value and exit 1 for any one of failure case in if condition for value comparision?

Sample data & snippet code is mentioned below:

#!/usr/bin/perl use warnings; use strict; use feature "switch"; my %Hash = (); $Hash{"FEB"}{"W1"}=10.14; $Hash{"FEB"}{"W2"}=11.22; $Hash{"MAR"}{"W1"}=33.17; $Hash{"MAR"}{"W2"}=44.44; $Hash{"APR"}{"W1"}=55.00; $Hash{"APR"}{"W2"}=66.66; my $FEB_L1_W1 = 11; my $FEB_U1_W2 = 13; my $APR_L1_W1 = 1; my $APR_U1_W2 = 13.02; for my $month ( keys %Hash ) { print "$month: "; for my $week ( keys %{ $Hash{$month} } ) { print "$week=$Hash{$month}{$week} \n"; given ($month) { when ('FEB') { if ( $week eq "W1" ) { if ( ( $FEB_L1_W1 <= $Hash{$month}{$week} ) and ( $Hash{$mont +h}{$week} <= $FEB_U1_W2 ) ) { print "month $month has f status for $week: $Hash{$month} +{$week}\n"; } else { print "error: print something"; } } elsif ( $week eq "W2" ) { if ( ( $FEB_U1_W2 <= $Hash{$month}{$week} ) and ( $Hash{$month}{$week} <= $FEB_U1_W2 ) ) { print "month $month has f status for $week: $Hash +{$month}{$week}\n"; } else { print "error: print something\n"; } } } when ('APR') { # same as above check for lower , upper values print "it is $month here\n";} } } }

In reply to 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.