Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Oops, I had a mistake there; that last print statement should print $sizes{$k} for the file size, instead of printing the number of times the size was found.

And here's a version that does it while only going through the list once. It does use an extra hash to keep track of matches, though, and it's definitely more complicated, so it probably isn't worth the trouble unless you have a large amount of data -- perhaps enough that reading it all into memory in one hash would be prohibitive, so going through it twice would hit the disk twice.

It works by saving the file sizes to a hash like before, but then when the same size is encountered again, the first file that had that size is printed, and the second one is 'promoted' to a second hash, which is also checked for matches against future sizes. That's the best way I could come up with to print matches as I went, but get the leftover matches printed at the end, and not reprint any.

#!/usr/bin/env perl my %files = qw( a 111 b 222 c 333 d 333 e 222 f 222); my %sizes_seen; # file sizes that have already been seen my %sizes_matched; # sizes that have already been matched, # and the last filename with that size for my $file (keys %files){ my $size = $files{$file}; # readability variable if( $sizes_matched{$size} ){ print "$sizes_matched{$size} => $size\n"; $sizes_matched{$size} = $file; } elsif( $sizes_seen{$size} ){ print "$sizes_seen{$size} => $size\n"; $sizes_matched{$size} = $file; } $sizes_seen{$size} = $file; } # finish printing the remaining matches for my $s (keys %sizes_matched){ print "$sizes_matched{$s} => $s\n"; }

In reply to Re^2: show me key, value when values are duplicate by aaron_baugher
in thread show me key, value when values are duplicate by ezekieldas

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (13)
    As of 2014-12-17 21:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (34 votes), past polls