my @ordered_result_list = sort keys %results;

That's wrong. You're sorting lexically, so 10 comes before 9.

print sort 1..10; # 1 10 2 3 4 5 6 7 8 9

The above is equivalent to

print sort { $a cmp $b } 1..10; # 1 10 2 3 4 5 6 7 8 9

You want to sort numerically.

print sort { $a <=> $b } 1..10; # 1 2 3 4 5 6 7 8 9 10

Of course, there's no reason to sort the result if you just want the highest.

use List::Util qw( max ); print max 1..10; # 10

my %results = map { $_ => 1 if ( $low_watermark < $_ && $_ < $high_wat +ermark ) } @all_numbers;

That's wrong. You're incorrectly assuming the expression in the map returns nothing when the condition is false.

$ perl -wle'my %h = map { $_ => 1 if $_ > 5; } 1..3; print 0+keys(%h); +' Odd number of elements in hash assignment at -e line 1. 1

You wanted:

my %results = map { if ( $low_watermark < $_ && $_ < $high_watermark ) + { $_ => 1 } else { () } } @all_numbers;

Or:

my %results = map { ( $low_watermark < $_ && $_ < $high_watermark ) ? +$_ => 1 : () } @all_numbers;

But why is a hash involved at all?

my @results = grep { $low_watermark < $_ && $_ < $high_watermark } @al +l_numbers; print max(@results), "\n";

In reply to Re^2: Array, element inside limits by ikegami
in thread Array, element inside limits by natol44

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.