zwon has pointed you in the right direction but I note that your output is not sorted by name but retains the order in which names first occured in the data. To keep that order you will need to preserve it in the hash and sort on it when printing.
use strict; use warnings; my $order = 0; my %names = (); while( <DATA> ) { my( $name, $value ) = split; $names{ $name }->{ order } = ++ $order unless exists $names{ $name }; push @{ $names{ $name }->{ values } }, $value; } print do { local $" = q{,}; qq{$_ @{ $names{ $_ }->{ values } }\n}; } for sort { $names{ $a }->{ order } <=> $names{ $b }->{ order } } keys %names; __END__ jim 14 john 23 ernest 38 matilda 43 jim 34 ernest 27 john 44 matilda 22
The output.
jim 14,34 john 23,44 ernest 38,27 matilda 43,22
I hope this is of interest.
Cheers,
JohnGG
In reply to Re: How can I group lines in a file?
by johngg
in thread How can I group lines in a file?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |