Andrej Kastrin wrote:
John Doe wrote:
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
many many thanks forall suggestions...
Cheers, Andrej
Dear helpers,
me, again. But please, I'm just learning and I try not to ask too much,
but there are some problems that are really hard to understand (inspite
of two big books and a lot of tutorial prints on my table).
So, what is the intuition to combine | merge | join 2 tables. E.g., if
we have table;
ID001 W1, W2, ...
ID002 W5, W9, ...
ID003 W3, W2, W10, ...
Then the second table looks like:
W1 text1, text2, text3
W2 text2, text5, text1
W3 text3, text4
The result must be:
ID001 text1,text2,text3,text5 #combine elements from W1 and W2 from
first table
ID002 ...
ID003 ...
I go to study "Perl Programming" now and thank's for all suggestions...
Cheers
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>