in reply to combine multiple files into one (line by line)
Note that this code assumes all your files have the same number of lines. If this is not the case, you have to do some extra trickery to not break out of the loop until all files are empty...my @files = ("1.txt", "2.txt", "3.txt"); my @fhs; foreach my $file (@files) { open($fh, $file) || die; push(@fhs, $fh); } open(MIXED, "mixed.txt") || die; while (1) { foreach $fh (@fhs) { $line = <$fh>; last if (eof($fh)); print MIXED $line; } } map { close($_); } @fhs; close(MIXED);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: combine multiple files into one (line by line)
by Anonymous Monk on Apr 05, 2001 at 22:53 UTC | |
by extremely (Priest) on Apr 05, 2001 at 23:36 UTC | |
by jeroenes (Priest) on Apr 05, 2001 at 23:49 UTC | |
by ton (Friar) on Apr 06, 2001 at 00:01 UTC |