Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I think you're using the wrong data structure. Hashes cannot have duplicate keys, and floating point numbers are a bad match for hash keys, anyway. Like: "0.456234" and "0.4562340" is the same numerical value, yet it's a different string (and thus: hash key).

I'd rather use an array of (value, url) tuples (maybe as anonymous arrays... With 2 loops, you can

  1. determine the max value
  2. fetch all tuples with that value.
Actually, if you do it smarter (but more complex), you can do it in one loop.
my @pairs = ([0.456234, 'url 1'], [0.42323, 'url 2'], [0.456234, 'url +3'], [0.456234, 'url 4']); my $max; my @url; foreach(@pairs) { if(!defined $max or $max < $_->[0]) { $max = $_->[0]; @url = $_->[1]; } elsif($max == $_->[0]) { # caution: exact identity with floati +ng point numbers is rare, so this might disappoint a little push @url, $_->[1]; } } use Data::Dumper; print Dumper (\@url);
p.s. your original data is wrong, as it's the second value that is the highest. I modified that value in my test code.

In reply to Re: hash map to give preference to 1st match by bart
in thread hash map to give preference to 1st match by sarvan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2023-12-11 12:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?