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";
|