in reply to merge two files

You are heading in the right direction. If you wrap the essence of your code in a for loop that iterates over the files you need to process and change the hash access from an assignment to a += then you've done the input part. Generating the output is then a matter of two nested loops. Consider:

use strict; use warnings; my $file1 = <<END_FILE1; 0: 0,11 5,6 11,2 1: 1,3 3: 0,1 2,2 3,2 5: 3,5 6,1 8,16 9,1 END_FILE1 my $file2 = <<END_FILE2; 0: 0,10 4,19 2: 1,3 2,5 6,4 5: 6,10 9,3 END_FILE2 my %sites; for my $source (\$file1, \$file2) { open my $inFile, '<', $source or die "Failed to open $source: $!\n +"; while (my $line = <$inFile>) { chomp $line; next if ! ($line =~ s/^([^:]+):\s*//); my $word = $1; for my $field (split ' ', $line) { my ($site, $count) = split /,/, $field; $sites{$word}{$site} += $count; } } } for my $word (sort {$a <=> $b} keys %sites) { print "$word: "; my $wordSites = $sites{$word}; for my $site (sort {$a <=> $b} keys %$wordSites) { print "$site,$wordSites->{$site} "; } print "\n"; }

Prints:

0: 0,21 4,19 5,6 11,2 1: 1,3 2: 1,3 2,5 6,4 3: 0,1 2,2 3,2 5: 3,5 6,11 8,16 9,4

There are a few things you ought do to save yourself time in the future. First off, always use strictures (use strict; use warnings;). Use the three parameter version of open (you didn't did you?) and always test the result of file opens. Use lexical file handles (open my $infile ...).

Note the indentation and bracketing style I've used in my sample. It is pretty common in Perl circles and is probably worth adopting. There is a really good tool called Perl Tidy that is well worth getting if you are at all interested in generating consistently formatted code.


True laziness is hard work