use File::Slurp; my $outputfile1 = "file1.txt" my $outputfile2 = "file2.txt" my $outputfile3 = "file3.txt" open (my $combined_file,">$outputfile3") or die "cannot create combined output file - $!"; my @a = read_file($outputfile1) or die "couldn't read $outputfile1 - $! [$^E]"; my @b = read_file($outputfile2) or die "couldn't read $outputfile2 - $! [$^E]"; my $combined = {}; # hashref my $i=0; foreach (@a) { chomp $_; $combined->{$i}{b} = '' unless defined $combined->{$i}{b}; $combined->{$i++}{a} = $_; } $i=0; foreach (@b) { chomp $_; $combined->{$i}{a} = '' unless defined $combined->{$i}{a}; $combined->{$i++}{b} = $_; } foreach my $i (sort {$a<=>$b} keys %$combined) { print $combined_file $combined->{$i}{a}, ("\n"),$combined->{$i}{b}; } } close($combined_file);