eric_newbie has asked for the wisdom of the Perl Monks concerning the following question:

I have this data contained in a file BIN COUNT 1 20 2 0 2 1 3 0 3 0 3 1 7 0 8 2 9 1 9 2 10 3 I split this and stored in a hash $hash{seq++} = { BIN => $bin_num, COUNT => $count, }; I wanted to merge the duplicate BINs into one and sum up their values (counts). The output would look like this: BIN COUNT 1 20 2 1 3 1 7 0 8 2 9 3 10 3 I was able to loop through hash and display it but cannot proceed with my desired output. I don't know how to do it. Please help!

Replies are listed 'Best First'.
Re: merge duplicate values in hash
by hdb (Monsignor) on Oct 10, 2013 at 09:18 UTC

    You might want to use the bin number as the key of the hash and add the count to it whenever you encounter it. It could look like this:

    use strict; use warnings; my %hash; my $data = "BIN COUNT 1 20 2 0 2 1 3 0 3 0 3 1 7 0 8 2 9 1 9 2 10 3"; $hash{$1} += $2 while $data =~ /(\d+) (\d+)/g; print "BIN COUNT "; print "$_ $hash{$_} " for sort {$a<=>$b} keys %hash;

    Also, please use code tags in your post to make it more readible.

Re: merge duplicate values in hash
by kcott (Archbishop) on Oct 10, 2013 at 09:34 UTC

    G'day eric_newbie,

    Welcome to the monastery.

    $hash{$seq++} = { BIN => $bin_num, COUNT => $count, }; creates a complex hash of hashes structure. I don't believe this is what you want; it's probably the reason you were unable to progresas your code beyond this point.

    I would've started by storing the file data in a simple array; then used a simple hash for the merge.

    #!/usr/bin/env perl -l use strict; use warnings; my @init = qw{1 20 2 0 2 1 3 0 3 0 3 1 7 0 8 2 9 1 9 2 10 3}; print "@init"; my %merge; $merge{$init[$_]} += $init[$_+1] for grep { not $_ % 2 } 0 .. $#init; my @out = map { $_, $merge{$_} } sort { $a <=> $b } keys %merge; print "@out";

    Output:

    1 20 2 0 2 1 3 0 3 0 3 1 7 0 8 2 9 1 9 2 10 3 1 20 2 1 3 1 7 0 8 2 9 3 10 3

    [For future reference, please see the guidelines in "How do I post a question effectively?".]

    -- Ken

Re: merge duplicate values in hash
by LanX (Saint) on Oct 10, 2013 at 09:18 UTC
Re: merge duplicate values in hash
by Corion (Patriarch) on Oct 10, 2013 at 09:11 UTC

    What purpose does $hash{$seq++}= ... serve? Please explain this in your own words.

    Maybe you can think about how a hash basically maps a "name" (for example, a bin) to a value (for example, the current count).