| 1 | package Text::Table::TSV; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | our $DATE = '2019-01-17'; # DATE | |||||||||||||
| 4 | 1 | our $VERSION = '0.002'; # VERSION | |||||||||||||
| 5 | |||||||||||||||
| 6 | #IFUNBUILT | ||||||||||||||
| 7 | # # use 5.010001; | ||||||||||||||
| 8 | # # use strict; | ||||||||||||||
| 9 | # # use warnings; | ||||||||||||||
| 10 | #END IFUNBUILT | ||||||||||||||
| 11 | |||||||||||||||
| 12 | 0 | ▶ | sub _encode { | ||||||||||||
|
Subroutine
_encode: not called CC=1 SCAR=6.9
| |||||||||||||||
| 13 | 0 | my $val = shift; | |||||||||||||
| 14 | 0 | $val =~ s/\t/ /g; | |||||||||||||
| 15 | 0 | $val; | |||||||||||||
| 16 | } | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 0 | ▶ | sub table { | ||||||||||||
|
Subroutine
table: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 19 | 0 | my %params = @_; | |||||||||||||
| 20 | 0 | ▶ | my $rows = $params{rows} or die "Must provide rows!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | 0 | my $max_index = _max_array_index($rows); | |||||||||||||
| 23 | |||||||||||||||
| 24 | # here we go... | ||||||||||||||
| 25 | 0 | my @table; | |||||||||||||
| 26 | |||||||||||||||
| 27 | # then the data | ||||||||||||||
| 28 | 0 | my $i = 0; | |||||||||||||
| 29 | 0 | foreach my $row ( @{ $rows }[0..$#$rows] ) { | |||||||||||||
| 30 | 0 | $i++; | |||||||||||||
| 31 | push @table, join( | ||||||||||||||
| 32 | "\t", | ||||||||||||||
| 33 | 0 | ▶ | map { _encode(defined($row->[$_]) ? $row->[$_] : '') } (0..$max_index) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | ), "\n"; | ||||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 0 | return join("", grep {$_} @table); | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | # FROM_MODULE: PERLANCAR::List::Util::PP | ||||||||||||||
| 41 | # BEGIN_BLOCK: max | ||||||||||||||
| 42 | 0 | ▶ | sub max { | ||||||||||||
|
Subroutine
max: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 43 | 0 | ▶ | return undef unless @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 0 | my $res = $_[0]; | |||||||||||||
| 45 | 0 | my $i = 0; | |||||||||||||
| 46 | 0 | ▶ | while (++$i < @_) { $res = $_[$i] if $_[$i] > $res } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 47 | 0 | $res; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | # END_BLOCK: max | ||||||||||||||
| 50 | |||||||||||||||
| 51 | # return highest top-index from all rows in case they're different lengths | ||||||||||||||
| 52 | 0 | ▶ | sub _max_array_index { | ||||||||||||
|
Subroutine
_max_array_index: not called CC=1 SCAR=6.9
| |||||||||||||||
| 53 | 0 | my $rows = shift; | |||||||||||||
| 54 | 0 | return max( map { $#$_ } @$rows ); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 1 | 1; | |||||||||||||
| 58 | # ABSTRACT: Generate TSV | ||||||||||||||
| 59 | |||||||||||||||
| 60 | __END__ | ||||||||||||||