line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Script::Toolbox::TableO; |
2
|
|
|
|
|
|
|
# vim: ts=4 sw=4 ai |
3
|
|
|
|
|
|
|
|
4
|
10
|
|
|
10
|
|
74
|
use strict; |
|
10
|
|
|
|
|
19
|
|
|
10
|
|
|
|
|
370
|
|
5
|
10
|
|
|
10
|
|
54
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
8860
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
11
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
12
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
13
|
|
|
|
|
|
|
@EXPORT = qw( |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Preloaded methods go here. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
21
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
22
|
|
|
|
|
|
|
sub new |
23
|
|
|
|
|
|
|
{ |
24
|
10
|
|
|
10
|
0
|
26
|
my $classname = shift; |
25
|
10
|
|
|
|
|
20
|
my $self = {}; |
26
|
10
|
|
|
|
|
30
|
bless( $self, $classname ); |
27
|
10
|
|
|
|
|
28
|
$self->_init( @_ ); |
28
|
10
|
|
|
|
|
30
|
return $self; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
32
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
33
|
|
|
|
|
|
|
sub _init |
34
|
|
|
|
|
|
|
{ |
35
|
10
|
|
|
10
|
|
29
|
my ($self, $param, $separator) = @_; |
36
|
|
|
|
|
|
|
|
37
|
10
|
50
|
|
|
|
25
|
return undef if( _noData( $param ) ); |
38
|
10
|
|
|
|
|
23
|
my $para = _normParam($param, $separator); |
39
|
|
|
|
|
|
|
|
40
|
10
|
|
|
|
|
75
|
my $form = Script::Toolbox::Util::Formatter->new( $para ); |
41
|
10
|
|
|
|
|
42
|
my $result= $form->matrix(); |
42
|
10
|
50
|
66
|
|
|
63
|
if( ref $param eq 'ARRAY' || !defined $param->{'sumCols'} ){ |
43
|
10
|
|
|
|
|
83
|
$self->{'result'} = $result; |
44
|
|
|
|
|
|
|
}else{ |
45
|
|
|
|
|
|
|
$self->{'result'} = $form->sumBy($result, $param->{'sumCols'}, |
46
|
0
|
|
|
|
|
0
|
$param->{'notGroupBy'}); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
51
|
|
|
|
|
|
|
# $param must be a hash reference. This Hash must have a key "data". |
52
|
|
|
|
|
|
|
# This key may point to: |
53
|
|
|
|
|
|
|
# arrayref |
54
|
|
|
|
|
|
|
# hashref |
55
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
56
|
|
|
|
|
|
|
sub _noData($) |
57
|
|
|
|
|
|
|
{ |
58
|
10
|
|
|
10
|
|
26
|
my ($param) = @_; |
59
|
|
|
|
|
|
|
|
60
|
10
|
100
|
|
|
|
30
|
return 0 if( ref $param ne 'HASH' ); |
61
|
6
|
100
|
|
|
|
18
|
return 0 if( ref $param->{'data'} eq 'HASH' ); |
62
|
5
|
50
|
|
|
|
30
|
return 0 if( ref $param->{'data'} eq 'ARRAY'); |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
0
|
if( !defined $param->{'data'}[0] ) |
65
|
|
|
|
|
|
|
{ |
66
|
0
|
|
|
|
|
0
|
Log( "WARNING: no input data for Table()." ); |
67
|
0
|
|
|
|
|
0
|
return 1; |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
0
|
return 0; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
73
|
|
|
|
|
|
|
# Valid Calls: |
74
|
|
|
|
|
|
|
# [ "csvString", "csvString",...], undef |
75
|
|
|
|
|
|
|
# [ "csvString", "csvString",...], separatorString |
76
|
|
|
|
|
|
|
# [ "TitelString", [headArray], [dataArray],...], undef |
77
|
|
|
|
|
|
|
# [ [dataArray],...], undef |
78
|
|
|
|
|
|
|
# {title=>"", head=>[], data=>[[],[],...] }, undef |
79
|
|
|
|
|
|
|
# {title=>"", head=>[], data=>[{},{},...] }, undef |
80
|
|
|
|
|
|
|
# {title=>"", head=>[], data=>{r1=>{c1=>,c2=>,},r2=>{c1=>,c2=>,},}, undef |
81
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
82
|
|
|
|
|
|
|
sub _normParam($$) |
83
|
|
|
|
|
|
|
{ |
84
|
10
|
|
|
10
|
|
26
|
my ($param, $separator) = @_; |
85
|
|
|
|
|
|
|
|
86
|
10
|
100
|
|
|
|
34
|
if( ref $param eq 'HASH' ) |
87
|
|
|
|
|
|
|
{ |
88
|
|
|
|
|
|
|
# keine Ahnung wozu: return _sepHash($param, $separator) if( _isCSV($param->{'data'}) ); |
89
|
6
|
|
|
|
|
13
|
return $param; |
90
|
|
|
|
|
|
|
} |
91
|
4
|
100
|
|
|
|
32
|
return _sepTitleHead($param) if( _isTitleHead($param) ); |
92
|
3
|
50
|
|
|
|
16
|
return _sepCSV($param, $separator) if( _isCSV($param, $separator) ); |
93
|
0
|
|
|
|
|
0
|
return { 'data' => $param }; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
97
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
98
|
|
|
|
|
|
|
sub _sepHash($$) |
99
|
|
|
|
|
|
|
{ |
100
|
0
|
|
|
0
|
|
0
|
my ($param,$separator) = @_; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
0
|
my $d = _sepCSV($param->{'data'}, $separator); |
103
|
0
|
|
|
|
|
0
|
$param->{'data'} = $d->{'data'}; |
104
|
0
|
|
|
|
|
0
|
return $param; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
108
|
|
|
|
|
|
|
# Check if we found the special data array format. |
109
|
|
|
|
|
|
|
# ["TitleString", [headString,headString,...],[data,...],...] |
110
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
111
|
|
|
|
|
|
|
sub _isTitleHead($) |
112
|
|
|
|
|
|
|
{ |
113
|
4
|
|
|
4
|
|
25
|
my ($param) = @_; |
114
|
|
|
|
|
|
|
|
115
|
4
|
100
|
66
|
|
|
54
|
return 1 if( ref \$param->[0] eq 'SCALAR' && ref $param->[1] eq 'ARRAY' ); |
116
|
3
|
|
|
|
|
11
|
return 0; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
120
|
|
|
|
|
|
|
# Transform the special data array |
121
|
|
|
|
|
|
|
# ["TitleString", [headString,headString,...],[data,...],...] |
122
|
|
|
|
|
|
|
# into hash format. |
123
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
124
|
|
|
|
|
|
|
sub _sepTitleHead($) |
125
|
|
|
|
|
|
|
{ |
126
|
1
|
|
|
1
|
|
11
|
my ($param) = @_; |
127
|
|
|
|
|
|
|
|
128
|
1
|
|
|
|
|
3
|
my $title= splice @{$param}, 0,1; |
|
1
|
|
|
|
|
15
|
|
129
|
1
|
|
|
|
|
8
|
my $head = splice @{$param}, 0,1; |
|
1
|
|
|
|
|
9
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
return { |
132
|
1
|
|
|
|
|
9
|
'title' => $title, |
133
|
|
|
|
|
|
|
'head' => $head, |
134
|
|
|
|
|
|
|
'data' => $param |
135
|
|
|
|
|
|
|
}; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
140
|
|
|
|
|
|
|
# [[],[],...] |
141
|
|
|
|
|
|
|
# [{},{},...] |
142
|
|
|
|
|
|
|
# {r1=>{c1=>,c2=>,},r2=>{c1=>,c2=>,},} |
143
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
144
|
|
|
|
|
|
|
sub _isCSV($$) |
145
|
|
|
|
|
|
|
{ |
146
|
3
|
|
|
3
|
|
7
|
my ($param, $separator) = @_; |
147
|
|
|
|
|
|
|
|
148
|
3
|
50
|
|
|
|
16
|
return 0 if( ref $param ne 'ARRAY' ); |
149
|
|
|
|
|
|
|
|
150
|
3
|
100
|
|
|
|
17
|
$separator = ';' unless defined $separator; #FIXME default sep |
151
|
3
|
50
|
|
|
|
95
|
return 1 if( $param->[0] =~ /$separator/ ); #assume it's a CSV record |
152
|
0
|
|
|
|
|
0
|
return 0; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
156
|
|
|
|
|
|
|
# Convert an array of CSV strings into an array of arrays. |
157
|
|
|
|
|
|
|
# |
158
|
|
|
|
|
|
|
# [ "a;b","c,d"] becomes |
159
|
|
|
|
|
|
|
# [[a,b], [c,d]] |
160
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
161
|
|
|
|
|
|
|
sub _sepCSV($$) |
162
|
|
|
|
|
|
|
{ |
163
|
3
|
|
|
3
|
|
12
|
my ($param, $separator) = @_; |
164
|
|
|
|
|
|
|
|
165
|
3
|
100
|
|
|
|
14
|
$separator = ';' if( !defined $separator); |
166
|
3
|
|
|
|
|
6
|
my @R; |
167
|
3
|
|
|
|
|
9
|
foreach my $l ( @{$param} ) |
|
3
|
|
|
|
|
11
|
|
168
|
|
|
|
|
|
|
{ |
169
|
9
|
|
|
|
|
115
|
my @r = split /$separator/, $l; |
170
|
9
|
|
|
|
|
22
|
push @R, \@r; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
3
|
|
|
|
|
11
|
return { 'data' => \@R }; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
177
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
178
|
|
|
|
|
|
|
sub asArray($){ |
179
|
10
|
|
|
10
|
0
|
20
|
my ($self) = @_; |
180
|
|
|
|
|
|
|
|
181
|
10
|
|
|
|
|
14
|
return @{$self->{'result'}}; |
|
10
|
|
|
|
|
35
|
|
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
185
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
186
|
|
|
|
|
|
|
sub asString($$){ |
187
|
0
|
|
|
0
|
0
|
|
my ($self,$sep) = @_; |
188
|
|
|
|
|
|
|
|
189
|
0
|
0
|
|
|
|
|
$sep = defined $sep ? $sep : "\n"; |
190
|
0
|
|
|
|
|
|
return sprintf "%s", join $sep, @{$self->{'result'}}; |
|
0
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
1; |
194
|
|
|
|
|
|
|
__END__ |