| 1 | package Script::Toolbox::TableO; | ||||||||||||||||||||||||||||
| 2 | # vim: ts=4 sw=4 ai | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 21 | require Exporter; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 21 | @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 | 21 | @EXPORT = qw( | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Preloaded methods go here. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 21 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 22 | sub new | ||||||||||||||||||||||||||||
| 23 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 10 | my $classname = shift; | |||||||||||||||||||||||||||
| 25 | 10 | my $self = {}; | |||||||||||||||||||||||||||
| 26 | 10 | bless( $self, $classname ); | |||||||||||||||||||||||||||
| 27 | 10 | $self->_init( @_ ); | |||||||||||||||||||||||||||
| 28 | 10 | return $self; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 32 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 33 | sub _init | ||||||||||||||||||||||||||||
| 34 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||
| 35 | 10 | my ($self, $param, $separator) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 10 | ▶ | return undef if( _noData( $param ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 10 | my $para = _normParam($param, $separator); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 10 | my $form = Script::Toolbox::Util::Formatter->new( $para ); | |||||||||||||||||||||||||||
| 41 | 10 | my $result= $form->matrix(); | |||||||||||||||||||||||||||
| 42 | 10 | ▶ | if( ref $param eq 'ARRAY' || !defined $param->{'sumCols'} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $param eq "ARRAY" or not defined $param->{"sumCols"}
MC/DC0%0/2
ref $param eq "ARRAY" or not defined $param->{"sumCols"}
ref $param eq "ARRAY"
not defined $param->{"sumCols"}
Truth table66%2/3
A or not(B)
A= ref $param eq "ARRAY"B= not defined $param->{"sumCols"}
| |||||||||||||||||||||||||||||
| 43 | 10 | $self->{'result'} = $result; | |||||||||||||||||||||||||||
| 44 | }else{ | ||||||||||||||||||||||||||||
| 45 | $self->{'result'} = $form->sumBy($result, $param->{'sumCols'}, | ||||||||||||||||||||||||||||
| 46 | 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 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_noData: called CC=5 SCAR=19.6
| |||||||||||||||||||||||||||||
| 58 | 10 | my ($param) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 10 | ▶ | return 0 if( ref $param ne 'HASH' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 6 | ▶ | return 0 if( ref $param->{'data'} eq 'HASH' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 5 | ▶ | return 0 if( ref $param->{'data'} eq 'ARRAY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if( !defined $param->{'data'}[0] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | 0 | Log( "WARNING: no input data for Table()." ); | |||||||||||||||||||||||||||
| 67 | 0 | return 1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 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 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_normParam: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 84 | 10 | my ($param, $separator) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 10 | ▶ | if( ref $param eq 'HASH' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||
| 88 | # keine Ahnung wozu: return _sepHash($param, $separator) if( _isCSV($param->{'data'}) ); | ||||||||||||||||||||||||||||
| 89 | 6 | return $param; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 4 | ▶ | return _sepTitleHead($param) if( _isTitleHead($param) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | return _sepCSV($param, $separator) if( _isCSV($param, $separator) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | return { 'data' => $param }; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 97 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 98 | sub _sepHash($$) | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_sepHash: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($param,$separator) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | my $d = _sepCSV($param->{'data'}, $separator); | |||||||||||||||||||||||||||
| 103 | 0 | $param->{'data'} = $d->{'data'}; | |||||||||||||||||||||||||||
| 104 | 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 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_isTitleHead: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 113 | 4 | my ($param) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | return 1 if( ref \$param->[0] eq 'SCALAR' && ref $param->[1] eq 'ARRAY' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref \$param->[0] eq "SCALAR" and ref $param->[1] eq "ARRAY"
MC/DC50%1/2
ref \$param->[0] eq "SCALAR" and ref $param->[1] eq "ARRAY"
ref \$param->[0] eq "SCALAR"
ref $param->[1] eq "ARRAY"
Truth table66%2/3
A and B
A= ref \$param->[0] eq "SCALAR"B= ref $param->[1] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 116 | 3 | 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 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_sepTitleHead: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | 1 | my ($param) = @_; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1 | my $title= splice @{$param}, 0,1; | |||||||||||||||||||||||||||
| 129 | 1 | my $head = splice @{$param}, 0,1; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | return { | ||||||||||||||||||||||||||||
| 132 | 1 | '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 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_isCSV: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||
| 146 | 3 | my ($param, $separator) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3 | ▶ | return 0 if( ref $param ne 'ARRAY' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 3 | ▶ | $separator = ';' unless defined $separator; #FIXME default sep | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 3 | ▶ | return 1 if( $param->[0] =~ /$separator/ ); #assume it's a CSV record | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 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 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_sepCSV: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 163 | 3 | my ($param, $separator) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 3 | ▶ | $separator = ';' if( !defined $separator); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 3 | my @R; | |||||||||||||||||||||||||||
| 167 | 3 | foreach my $l ( @{$param} ) | |||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||
| 169 | 9 | my @r = split /$separator/, $l; | |||||||||||||||||||||||||||
| 170 | 9 | push @R, \@r; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 3 | return { 'data' => \@R }; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 177 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 178 | 10 | ▶ | sub asArray($){ | ||||||||||||||||||||||||||
|
Subroutine
asArray: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 179 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 10 | return @{$self->{'result'}}; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 185 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub asString($$){ | ||||||||||||||||||||||||||
|
Subroutine
asString: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 187 | 0 | my ($self,$sep) = @_; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | $sep = defined $sep ? $sep : "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | return sprintf "%s", join $sep, @{$self->{'result'}}; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 21 | 1; | |||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||