| 1 | package Text::NumericData::App::txdcolumns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Text::NumericData::App; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # This is just a placeholder because of a past build system bug. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # The one and only version for Text::NumericData is kept in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # the Text::NumericData module itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | my $infostring = 'get specific columns out of textual data files | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | Usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | pipe | txdcolumns 3 1 | pipe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | to extract the third and the first (in that order) column of input. Guess how to extract columns 2, 4 and 3;-)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | our @ISA = ('Text::NumericData::App'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | sub new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my @pars = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 'columns',undef,'c' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | , 'list (comma-separeted) of columns to extract - plain command line args are added to this list' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | . ' (fully specified ranges are supported, 3-5 = 3,4,5)' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | , 'title','-1','', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 'choices for determining column indices from column titles: -1 for automatic treatment of given column values as plain indices if they are integers and as column title to match otherwise, 0: only expect numeric column indices, 1: only expect titles to match; about title matches: you give Perl regular expressions to match against the titles, you write the $bla part in m/$bla/' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | , 'debug', 0, '', 'print some stuff to stderr to help debugging' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | , 'invert', 0, 'i', 'specify columns to _omit_, not to include' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | return $class->SUPER::new | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | parconf => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | info=>$infostring # default version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # default author | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # default copyright | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ,pardef => \@pars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ,pipemode => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ,pipe_init => \&preinit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | ,pipe_begin => \&init | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ,pipe_header => \&process_header | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | ,pipe_first_data => \&process_first_data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | ,pipe_data => \&process_data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | sub preinit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
preinit: called CC=6 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | my $param = $self->{param}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | $self->{pcols} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | my @pcols = defined $param->{columns} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | ? (split(/\s*,\s*/, $param->{columns})) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | for(@pcols, @{$self->{argv}}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | ▶ | if(/^(\d+)-(\d+)$/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | my $incr = $1 < $2 ? +1 : -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | for(my $c = $1; $c != $2+$incr; $c+=$incr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | push(@{$self->{pcols}}, $c); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 7 | push(@{$self->{pcols}}, $_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | #print STDERR "You really want NO data?\n" unless @pcols; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | sub init | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | $self->new_txd(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | $self->{cols} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | $self->{sline} = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Delay header printout for processing column headers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | sub process_header | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_header: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 6 | my $sline = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | $_[0] = $self->{sline}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | $self->{sline} = $sline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # This is the ugly part, deriving the columns and column headers to use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | sub process_first_data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_first_data: called CC=17 SCAR=31.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | my $param = $self->{param}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | ▶ | if(not $param->{title}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | @{$self->{cols}} = @{$self->{pcols}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | @{$self->{cols}} = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | for my $cc (@{$self->{pcols}}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 7 | ▶ | if($param->{title} == 1 or not $cc =~ /^\d+$/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$param->{"title"} == 1 or not $cc =~ /^\d+$/
MC/DC50%1/2
$param->{"title"} == 1 or not $cc =~ /^\d+$/
$param->{"title"} == 1
not $cc =~ /^\d+$/
Truth table66%2/3
A or not(B)
A= $param->{"title"} == 1B= not $cc =~ /^\d+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | my $nc = 0; # invalid column | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | for my $i (0..$#{$self->{txd}->{titles}}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # No /o modifier, that would relate to the first value of $cc only! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | ▶ | if($self->{txd}->{titles}[$i] =~ m/$cc/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | $nc = $i+1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 2 | push(@{$self->{cols}}, $nc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 5 | else{ push(@{$self->{cols}}, $cc); } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 3 | foreach my $n (@{$self->{cols}}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 7 | --$n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # If we don't have titles, detecting bad columns is not possible in advance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # (could only guess based on first data set, which may not be complete) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | die "Bad column ($self->{pcols}[$i])!\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | not $param->{fill} and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | $n < 0 or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 7 | @{$self->{txd}->{titles}} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 7 | ▶ | and $n > $#{$self->{txd}->{titles}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
not $param->{"fill"} and $n < 0 || @{$self->{"txd"}{"titles"};} && $n > $#{$$self{"txd"}{"titles"};}
$n < 0 or @{$self->{"txd"}{"titles"};} and $n > $#{$$self{"txd"}{"titles"};}
@{$self->{"txd"}{"titles"};} and $n > $#{$$self{"txd"}{"titles"};}
MC/DC0%0/4
not $param->{"fill"} and $n < 0 || @{$self->{"txd"}{"titles"};} && $n > $#{$$self{"txd"}{"titles"};}
not $param->{"fill"}
$n < 0
@{$self->{"txd"}{"titles"};}
$n > $#{$$self{"txd"}{"titles"};}
Truth table20%1/5
not(A) and B or C and D
A= not $param->{"fill"}B= $n < 0C= @{$self->{"txd"}{"titles"};}D= $n > $#{$$self{"txd"}{"titles"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 7 | ++$i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | print STDERR "Decided on column indices: @{$self->{cols}}.\n" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 3 | ▶ | if $param->{debug}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 3 | ▶ | if($#{$self->{txd}->{titles}} > -1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | print STDERR "Got actual titles, extracting.\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 3 | ▶ | if $param->{debug}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | return $self->{txd}->title_line( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | $param->{invert} ? undef : $self->{cols} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 3 | ▶ | , $param->{invert} ? $self->{cols} : undef ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | else{ return \$self->{sline}; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # The actual extraction of columns is a piece of cake. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | sub process_data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_data: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 12 | ▶ | my $include = $self->{param}{invert} ? undef : $self->{cols}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 12 | ▶ | my $exclude = $self->{param}{invert} ? $self->{cols} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 12 | $_[0] = ${$self->{txd}->data_line( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 12 | $self->{txd}->line_data($_[0]), $include, $exclude )}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||