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