open FH, "first file" || die "problem with first file : $!"; @first =; # slurp into array close FH; open FH, "next file" || die "problem with first file : $!"; @second =; # slurp into array close FH; # at this point, we've got the lines of each file in two different arrays. # if it's important that they have the same number of lines, you can # check with if (scalar (@first) != scalar (@second)) {... # # now start output to the third file. maybe check to see if it exists? open OUTPUT, ">output.txt"; # since we're reading from 2 different arrays # it's easier to use for (0..$#first) rather than foreach (@first) # so that we can apply the element number to both arrays. # my $sep=","; # specifies comma separated output. this might be bad if your data has commas in it. for (0..$#first) { # this is a liability if the second file has more lines... print OUTPUT join $sep, split (/\s+?/, $first($_)), split (/\s+?/, $second($_)); } close OUTPUT;