You have a good start on reading the first file. Now you just need to use the same hash when reading the second file, and do:
instead of just the straight "=" assignment. That will make sure that when the second file contains a "word" and "site" combination that was also in the first file, you'll be adding together the two "count" values.$file1{$word}{$site} += $count;
You should also start with use strict; and it will make sense to have your file reading logic in a subroutine that you call for each file, just to avoid repeating code unnecessarily:
(not tested)use strict; my %data; my @filenames = ...; # @ARGV or literal strings or whatever for my $fname ( @filenames ) { open my $input, "<", $fname or die "$fname: $!"; load_data( $input, \%data ); } # doing stuff with %data is left as an exercise... sub load_data { my ( $fh, $href ) = @_; while ( <$fh> ) { next unless ( s/^(.*?):\s*// ); my $word = $1; for my $field ( split ) { my ( $site, $count ) = split /,/, $field; $$href{$word}{$site} += $count; } } }
In reply to Re: merge two files
by graff
in thread merge two files
by karey3341
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |