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

In reply to Re: merge two files by GrandFather
in thread merge two files by karey3341

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.