Thanks for your quick reply, but adding chomp after the declaration of each while loop has the same effect as adding after $_ =~ tr/\n/ /; after the values have been swapped.
In that is removes all of the carriage returns and I end up with a continious stream of text.
However your suggestion pointed the way to the solution, I've added chomp after the declaration of the second while loop and that appears to have fixed my problem.