Hi,
I just read this thread again and saw your reply.
Assuming, that an empty string is not a valid value, I came up with this:
#! /usr/bin/perl
use strict;
use warnings;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new({ binary => 1, allow_whitespace => 1, })
or die "Cannot use CSV: " . Text::CSV_XS->error_diag();
# for testing; in real world, open file and use that handle
my $fh = \*DATA;
my (%hash, @hdr);
while ( my $row = $csv->getline( $fh ) ) {
# header not yet defined? or 1st cell starts with '<' ==> use row
+as header
if ( !@hdr || $row->[0] =~ m/^</ ) {
@hdr = @{$row};
next;
}
# otherwise try to process data
else {
for my $i ( 0 .. $#hdr ) {
# only add those values which contain at least one charact
+er
# so: no "undef"s or empty strings in result
# if empty strings are OK or wanted, try to replace length
+() with defined()
push @{ $hash{$hdr[$i]} }, ( length $row->[$i] ? $row->[$i
+] : () );
}
}
}
# check created data structure
require Data::Dumper;
$Data::Dumper::Sortkeys = 1;
print Data::Dumper::Dumper( \%hash );
__DATA__
<A1>, <A2>, <A3>
a1, aa1, aaa1
a2, , aaa2
a3, aa3
a4
<B1>, <B2>
b1, bb1
b2, bb2
b3
That produced a result like this:
$VAR1 = {
'<A1>' => [
'a1',
'a2',
'a3',
'a4'
],
'<A2>' => [
'aa1',
'aa3'
],
'<A3>' => [
'aaa1',
'aaa2'
],
'<B1>' => [
'b1',
'b2',
'b3'
],
'<B2>' => [
'bb1',
'bb2'
]
};