Revision 7ec8b55e
Von Bernd Blessmann vor fast 11 Jahren hinzugefügt
SL/Helper/Csv.pm | ||
---|---|---|
7 | 7 |
use Carp; |
8 | 8 |
use IO::File; |
9 | 9 |
use Params::Validate qw(:all); |
10 |
use List::MoreUtils qw(all); |
|
10 |
use List::MoreUtils qw(all pairwise);
|
|
11 | 11 |
use Text::CSV_XS; |
12 | 12 |
use Rose::Object::MakeMethods::Generic scalar => [ qw( |
13 | 13 |
file encoding sep_char quote_char escape_char header profile |
14 | 14 |
numberformat dateformat ignore_unknown_columns strict_profile is_multiplexed |
15 |
_io _csv _objects _parsed _data _errors all_cvar_configs case_insensitive_header |
|
15 |
_row_header _io _csv _objects _parsed _data _errors all_cvar_configs case_insensitive_header
|
|
16 | 16 |
) ]; |
17 | 17 |
|
18 | 18 |
use SL::Helper::Csv::Dispatcher; |
... | ... | |
221 | 221 |
sub _header_by_row { |
222 | 222 |
my ($self, $row) = @_; |
223 | 223 |
|
224 |
my @header = @{ $self->header }; |
|
224 |
# initialize lookup hash if not already done |
|
225 |
if ($self->is_multiplexed && ! defined $self->_row_header ) { |
|
226 |
$self->_row_header({ pairwise { $a->{row_ident} => $b } @{ $self->profile }, @{ $self->header } }); |
|
227 |
} |
|
228 |
|
|
225 | 229 |
if ($self->is_multiplexed) { |
226 |
my $i = 0; |
|
227 |
foreach my $profile (@{ $self->profile }) { |
|
228 |
if ($row->[0] eq $profile->{row_ident}) { |
|
229 |
return $header[$i]; |
|
230 |
} |
|
231 |
$i++; |
|
232 |
} |
|
230 |
return $self->_row_header->{$row->[0]} |
|
233 | 231 |
} else { |
234 |
return $header[0];
|
|
232 |
return $self->header->[0];
|
|
235 | 233 |
} |
236 | 234 |
} |
237 | 235 |
|
SL/Helper/Csv/Dispatcher.pm | ||
---|---|---|
5 | 5 |
use Data::Dumper; |
6 | 6 |
use Carp; |
7 | 7 |
use Scalar::Util qw(weaken); |
8 |
use List::MoreUtils qw(all pairwise); |
|
8 | 9 |
use Rose::Object::MakeMethods::Generic scalar => [ qw( |
9 |
_specs _errors |
|
10 |
_specs _row_class _row_spec _errors
|
|
10 | 11 |
) ]; |
11 | 12 |
|
12 | 13 |
use SL::Helper::Csv::Error; |
... | ... | |
38 | 39 |
return $obj; |
39 | 40 |
} |
40 | 41 |
|
41 |
# return class for given line |
|
42 |
# if only one profile is given, return this profiles class |
|
43 |
# if more than one profile is given, identify class by first |
|
44 |
# column??? |
|
45 | 42 |
sub _class_by_line { |
46 | 43 |
my ($self, $line) = @_; |
47 | 44 |
|
48 |
my $class; |
|
45 |
# initialize lookup hash if not already done |
|
46 |
if ($self->_csv->is_multiplexed && ! defined $self->_row_class ) { |
|
47 |
$self->_row_class({ map { $_->{row_ident} => $_->{class} } @{ $self->_csv->profile } }); |
|
48 |
} |
|
49 |
|
|
49 | 50 |
if ($self->_csv->is_multiplexed) { |
50 |
foreach my $p (@{ $self->_csv->profile }) { |
|
51 |
my $row_ident = $p->{row_ident}; |
|
52 |
if ($line->{datatype} eq $row_ident) { |
|
53 |
$class = $p->{class}; |
|
54 |
last; |
|
55 |
} |
|
56 |
} |
|
51 |
return $self->_row_class->{$line->{datatype}}; |
|
57 | 52 |
} else { |
58 |
$class = $self->_csv->profile->[0]->{class};
|
|
53 |
return $self->_csv->profile->[0]->{class};
|
|
59 | 54 |
} |
60 |
|
|
61 |
return $class; |
|
62 | 55 |
} |
63 | 56 |
|
64 | 57 |
sub _specs_by_line { |
65 | 58 |
my ($self, $line) = @_; |
66 | 59 |
|
67 |
my $spec; |
|
68 |
my $i = 0; |
|
60 |
# initialize lookup hash if not already done |
|
61 |
if ($self->_csv->is_multiplexed && ! defined $self->_row_spec ) { |
|
62 |
$self->_row_spec({ pairwise { $a->{row_ident} => $b } @{ $self->_csv->profile }, @{ $self->_specs } }); |
|
63 |
} |
|
64 |
|
|
69 | 65 |
if ($self->_csv->is_multiplexed) { |
70 |
foreach my $p (@{ $self->_csv->profile }) { |
|
71 |
my $row_ident = $p->{row_ident}; |
|
72 |
if ($line->{datatype} eq $row_ident) { |
|
73 |
$spec = $self->_specs->[$i]; |
|
74 |
last; |
|
75 |
} |
|
76 |
$i++; |
|
77 |
} |
|
66 |
return $self->_row_spec->{$line->{datatype}}; |
|
78 | 67 |
} else { |
79 |
$spec = $self->_specs->[0];
|
|
68 |
return $self->_specs->[0];
|
|
80 | 69 |
} |
81 |
|
|
82 |
return $spec; |
|
83 | 70 |
} |
84 | 71 |
|
85 | 72 |
|
Auch abrufbar als: Unified diff
Lookup-Hashes für Header, Klassen und Specs bei Multiplex-Daten benutzen.