Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Array, element inside limits

by ikegami (Pope)
on Jun 15, 2009 at 17:40 UTC ( #771731=note: print w/replies, xml ) Need Help??


in reply to Re: Array, element inside limits
in thread Array, element inside limits

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

Replies are listed 'Best First'.
Re^3: Array, element inside limits
by RMGir (Prior) on Jun 16, 2009 at 11:53 UTC
    For that matter, why use max, since the natol44 specified "a sorted array of numbers"?
    print $results[-1],"\n";
    :)

    Mike
      Good catch. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://771731]
help
Chatterbox?
[choroba]: Algorithm::Loops
[Corion]: choroba: I'm using that to generate the permutations, but I don't know how the user can pass the intended values to my function in a sane way
[Corion]: I have a prototype that permutes the get_parameters, but the values used for the get parameters should be different from the values used for the headers and potentially for parts of the URL
[Corion]: But yes, in general, my approach will be "split the URL into another set of parameters, generate an array of allowed values for each parameter and then NestedLoops() over the set"
[choroba]: hmm... so you need something like bag from Test::Deep, but not for checking, but for generation
[Corion]: This has the dual use of easily requesting sequential URLs and also being suitable for testing
[Corion]: For testing, I want to skip all tests with the same value(s) once one test fails to cut down on the number of failing tests
[Corion]: choroba: Yes, in a way I
[Corion]: ... I'm treating the incoming value sets as bags... Maybe I'll just put the burden on the user, at least in the first attempt at a full API. generate( headers => [{ 'Content-Type' => 'text/plain' }, {'Content-Type' => 'text/json' ] ), even ...
[Corion]: ... if that is cumbersome for the caller to specify. On the other hand, maybe there isn't that big a need for permuting the header values

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-01-17 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (152 votes). Check out past polls.