Andrej Kastrin wrote:
Dear Perl users,

Howdy

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


Use the first column as a key to a hash and make the value an array ref:

my %end_results;

for my $line_arrayref(@records) {
    push @{ $end_results{$line_arrayref->[0]} }, $line_arrayref->[1]
}

now %end_results looks like:

  'id001' => ['text1', 'text2', 'text3'],
  'id002' => ['text23', 'text', '555'],
  'id003' => ['text666'],

You could do the same thing with map also...

HTH :)

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