open F, "file1" or die "Problem with file1"; open G, "file2" or die "Problem with file2"; @file1 = map { (split /\s+/, $_)[0]; } ; @file2 = map { (split /\s+/, $_)[0]; } ; while ($coord2 = shift @file2) { $coord1 = shift @file1; push @result, $coord1 - $coord2; unshift @file1, $coord1; } open H, "> result.txt" or die "Unable to open result.txt"; print H join ("\n", @result);