#declare all filenames $filename1 = 'one.txt'; $filename2 = 'two.txt'; $filename3 = 'three.txt'; #### #open text file 1 open (FILE1, $filename1) or die "Unable to open $filename1 because $!\n"; #### # Store the contents of the first file into $hash{col1_col2} open (FILE1, $filename1) or die "Unable to open $filename1 because $!\n"; while ($line = ) { chomp ($line); ($chrX, $chrpos, $value1, $value2) = split (/\t/, $line); $key1 = join ("_", $chrX, $chrpos); $hash{$key1}++; }; close FILE1; #### while ($line = ) { chomp ($line); ($chrX, $chrpos, $value11, $value22) = split (/\t/, $line); $key2 = join ("_", $chrX, $chrpos); if (exists $hash{$key2} > 0) { $hash{key2} = $value11 + $value1; $hash{key2} = $value22 + $value2; $hash{key2}++ } }; #### while ($line = ) { chomp ($line); ($chrX, $chrpos, $value11, $value22) = split (/\t/, $line); $key2 = join ("_", $chrX, $chrpos); if (exists $hash{$key2} > 0) { $hash{key2} = $value11 + $value1; $hash{key2} = $value22 + $value2; $hash{key2}++ } }; #### open (FILE2, $filename2) or die "Unable to open $filename2 because $!\n"; while ($line = ) { chomp ($line); ($chrX, $chrpos, $value11, $value22) = split (/\t/, $line); $key2 = join ("_", $chrX, $chrpos); if (exists $hash{$key2} > 0) { $hash{key2} = $value11 + $value1; $hash{key2} = $value22 + $value2; $hash{key2}++ } }; close FILE2; #### sub process_file { my $filename = shift or die "Missing filename!"; open (FILE, $filename) or die "Unable to open $filename because $!\n"; while ($line = ) { chomp ($line); ($chrX, $chrpos, $value11, $value22) = split (/\t/, $line); $key2 = join ("_", $chrX, $chrpos); if (exists $hash{$key2} > 0) { $hash{key2} = $value11 + $value1; $hash{key2} = $value22 + $value2; $hash{key2}++ } } close FILE; } #### process_file($filename2); process_file($filename3);