greetsathya has asked for the wisdom of the Perl Monks concerning the following question:
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";} } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simplify HoH code
by haukex (Archbishop) on Dec 03, 2017 at 10:08 UTC | |
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 | |
|
Re: Simplify HoH code
by pme (Monsignor) on Dec 03, 2017 at 11:04 UTC | |
by greetsathya (Initiate) on Dec 03, 2017 at 11:51 UTC | |
|
Re: Simplify HoH code
by Anonymous Monk on Dec 03, 2017 at 16:04 UTC | |
by Anonymous Monk on Dec 03, 2017 at 16:50 UTC | |
|