Jan Eden wrote:
Hi,

Hello again,

I had the following task: Open a file, read it and merge all pairs of lines containing 
a certain number of tabs. Example:

Blablabla
abc cab bca
123 453 756
Blablabla
Blablabla

Here, lines 2 and three should be merged, while the other lines should remain 
untouched. Expected result:

Blablabla
abc 123
cab 453
bca 756
Blablabla
Blablabla

While I managed to get this done, I doubt that I found a good (fast) solution. So 
before I move on to the large files which have to be processed, I'd like to get your 
input for a better solution.

[snip]

Of course if you *really* want to read the entire file into memory then you could do it like this:


#!/usr/bin/perl
use warnings;
use strict;

open FILE,    '<', 'file.txt'        or die "Cannot open 'file.txt' $!";
open OUTFILE, '>', 'input_file2.txt' or die "Cannot open 'input_file2.txt' $!";

( my $data = do { local $/; <FILE> } ) =~
    s{^([^\t]+)\t([^\t]+)\t([^\t]+)\n([^\t]+)\t([^\t]+)\t([^\t]+)\n}
     {$1\t$4\n$2\t$5\n$3\t$6\n}mg;

print OUTFILE $data;

__END__



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to