The following code assumes that the different data lines are consistent (i.e. for a given ID there is a unique non-empty value for each field):
use strict;
use warnings;
my %data;
while ( <DATA> ) {
my ( $id, @fields ) = split /\s*,\s*/, $_, -1;
for my $i ( 0..$#fields ) {
$data{ $id }[ $i ] = $fields[ $i ] if length $fields[ $i ];
}
}
__END__
Update:I've attempted to fix one of the problems pointed out by ikegami below (specifically, added the -1 argument to split). I still focus only on the problem of consolidating from various lines, which I think is what the OP was having problems with, and ingore the issue of output.