Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
No such thing as a small change
 
PerlMonks  

show me key, value when values are duplicate

by ezekieldas (Initiate)
on Oct 05, 2011 at 22:40 UTC ( #929897=perlquestion: print w/ replies, xml ) Need Help??
ezekieldas has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find files that are the same size in bytes. Some specific examples we're working with are: f.sst is 939, g-w0e.sst is 2031, f-f3i.sst is 939. So, I'm interested in the two files which equal 939. I want to print the file, it's size in bytes when there are two or more with matching bytes. To simplify, using this example:
@stuff = (a, 1, b, 2, c, 3, d, 3);
I'd like to see:
c => 3 d => 3
The closest I can get to this is...
@stuff = (a, 1, b, 2, c, 3, d, 3); push(@unique, foo, grep { $seen{$_}++ } @stuff); %unique = @unique; while ( ($key, $value) = each %unique) { print "$key => $value\n"; }

Comment on show me key, value when values are duplicate
Select or Download Code
Re: show me key, value when values are duplicate
by jwkrahn (Monsignor) on Oct 05, 2011 at 22:51 UTC
    $ perl -le' use warnings; use strict; my %stuff = ( "a", 1, "b", 2, "c", 3, "d", 3 ); my %by_size; for my $key ( keys %stuff ) { push @{ $by_size{ $stuff{ $key } } }, $key; } for my $size ( keys %by_size ) { print "Size = $size, Names = @{ $by_size{ $size } }"; } ' Size = 1, Names = a Size = 3, Names = c d Size = 2, Names = b
Re: show me key, value when values are duplicate
by aaron_baugher (Deacon) on Oct 06, 2011 at 01:36 UTC

    There's probably a way to do this while going through the data only once, but I can't think of it. Here's a method that goes through the values (file sizes) once and then through the keys. It saves the number of times each file size is seen, and then prints out the files that have sizes that were seen more than once.

    #!/usr/bin/perl my %sizes = qw( a 1 b 2 c 3 d 3 e 2 f 2); my %times_seen; for my $v (values %sizes){ $times_seen{$v} ? $times_seen{$v}++ : ($times_seen{$v} = 1); } for my $k (keys %sizes){ if($times_seen{$sizes{$k}} > 1 ){ print "$k => $times_seen{$sizes{$k}}\n"; } }

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://929897]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-04-18 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (472 votes), past polls