> -----Original Message-----
> From: John Doe [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, January 18, 2006 7:34 AM
> To: [email protected]
> Subject: Re: Transform column into row
>
> 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
> >
> > Thank's for any suggestions.
If you own a copy of Perl Cookbook, look at chapter 5.7 "Hashes with
Multiple Values Per Key"
"c:/brian/text_files/cook_5_7.txt" =
id001 text1
id001 text2
id001 text3
id002 text23
id002 text555
id003 text666
------- hash_mult_values.pl -----
#!/usr/bin/perl
use strict;
use warnings;
my %texts = ();
my $file = "c:/brian/text_files/cook_5_7.txt";
open (FILE, $file) or die "Can't open $file: $!";
while (<FILE>) {
my ($id, $text) = split;
push( @{$texts{$id}}, $text );
}
foreach my $id (sort keys %texts) {
print "$id: @{$texts{$id}}\n";
}
----- end hash_mult_values.pl ----
Gives the result:
C:\brian\perl>perl hash_mult_values.pl
id001: text1 text2 text3
id002: text23 text555
id003: text666
----
Hope this helps,
Brian Volk
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>