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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |