#!/usr/bin/perl -w use strict; use warnings; use Data::Dump qw/dump/; my %data; foreach my $filename (qw/one.txt two.txt three.txt/) { open( my $file, $filename ) or die "Unable to open $filename because $!\n"; while (<$file>) { chomp; my ( $chrX, $chrpos, $value1, $value2 ) = split(/\s+/); $data{$chrX}->{$chrpos}->{'value1'} += $value1; $data{$chrX}->{$chrpos}->{'value2'} += $value2; } ## end while (<$file>) } ## end foreach my $filename (qw/one.txt two.txt three.txt/) print dump( \%data ); #### one.txt chromosome1 50000 12 20 chromosome2 20000 0 21 chromosome3 41444 9 2 chromosome4 21414 4 1 . . . (there would be 5million lines of the above)