Andrej Kastrin am Mittwoch, 18. Januar 2006 10.49:
> Dear Perl users,
>
> what's the best way to  transform column table in row format. I know how
> to split each line according to delimiter and than put it separately
> into array, but I have more complicated problem (with multiple equal
> records in the first column)
>
> id001  text1
> id001  text2
> id001  text3
> id002  text23
> id002  text555
> id003  text666
>
> and want something like:
>
> id001 text1 text2 text3
> id002 text23 text 555
> id003 text666

Ok, in addition to my apologies, I present here the script I described in my 
last post.

In constrast to the other presented solutions using a a hash slurping all data 
from the intput file, the advantage of my solution is that it is 
*capable to handle 300GB input files* even with 128MB RAM. 


===
#!/usr/bin/perl

use strict;
use warnings;

# open input file here if source is not <DATA>

open (my $outf, '>', './andrey_kastrin.out.txt')
  or die "Can't open file: $!";

my $old_name='';
while (<DATA>) {
  chomp;
  my ($name, $value)=$_=~/^([\w]+)\s+(.*)$/;
  if ($old_name ne $name) {
    print $outf "\n" if $old_name;
    print $outf $_;
  }
  else {
    print $outf ' ',$value;
  }
  $old_name=$name;
}
close $outf or die "Can't close file: $!";

# close input file here if source is not <DATA>


__DATA__
id001  text1
id001  text2
id001  text3
id002  text23
id002  text555
id003  text666
===


It outputs:

id001  text1 text2 text3
id002  text23 text555
id003  text666

I'm a nice guy, see?

joe

-- 
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