Do I understand correctly that the variables named *_L1_* are lower limits and *_U1_* are upper limits? Although pme has shown code very similar to what I was thinking, the key thing to note is that you can restructure your $FEB_L1_W1 variables into a hash, and then see the similarity in the data structures (Data::Dumper or Data::Dump can also be helpful here).

my %months = ( FEB => { W1 => 11.14, W2 => 11.22, }, MAR => { W1 => 33.17, W2 => 44.44, }, APR => { W1 => 55.00, W2 => 66.66, }, ); my %limits = ( FEB => { W1 => { L1 => 11, U1 => 12.05 }, W2 => { L1 => 13.06, U1 => 13.06 }, }, APR => { W1 => { L1 => 1, U1 => 20 }, W2 => { L1 => 33, U1 => 13.02 }, }, MAR => { W1 => { L1 => -12.05, U1 => -14.0 }, W2 => { L1 => 22.0, U1 => -13.02 }, }, );

And then notice you can create loops to loop over both at the same time, like in the following. Note I've made a few assumptions, like that limits for a certain month may not exist, but when they do, both the keys U1 and L1 are defined. If your input data varies, you may have to adjust this.

for my $mon ( sort keys %months ) { for my $week ( sort keys %{ $months{$mon} } ) { if ( exists $limits{$mon} && exists $limits{$mon}{$week} ) { my $value = $months{$mon}{$week}; my $upper = $limits{$mon}{$week}{U1}; my $lower = $limits{$mon}{$week}{L1}; printf "%6.2f <= %6.2f <= %6.2f - ", $lower, $value, $upper; if ( $value >= $lower && $value <= $upper ) { print "OK\n"; } else { print "NOT ok\n"; } } else { warn "No limits for month $mon / week $week\n" } } }

Which gives the following output. Note I tweaked the input slightly to give at least one "OK" output.

1.00 <= 55.00 <= 20.00 - NOT ok 33.00 <= 66.66 <= 13.02 - NOT ok 11.00 <= 11.14 <= 12.05 - OK 13.06 <= 11.22 <= 13.06 - NOT ok -12.05 <= 33.17 <= -14.00 - NOT ok 22.00 <= 44.44 <= -13.02 - NOT ok

In reply to Re^3: 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.