| 1 | package Text::NumericData::App::txdrows; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Text::NumericData; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Text::NumericData::Calc qw(expression_function); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Text::NumericData::App; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # This is just a placeholder because of a past build system bug. | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # The one and only version for Text::NumericData is kept in | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # the Text::NumericData module itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '1'; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | my $infostring = 'split a section of sets out of singe data file | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | Usage: | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | pipe | txdrows -b=3 -e=10 | pipe | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | pipe | txdrows \'near([1], 3.1)\' | pipe | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | This program extracs rows (data sets, sections, records, ...) of a numerical text file. Either this is a configured range of lines via begin/end indices or a decimation factor, or a set of rows matching a given expression on the command line. | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | The last example employs such an expression to match the first row that has a value near 3.1 in the first column. You could specify a third argument to near() to change the default allowed deviation. If you deal with integer values, using | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | pipe | txdrows \'[1] == 3\' | pipe | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | is fine, too, for selecting value 3.'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | our @ISA = ('Text::NumericData::App'); | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | sub new | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | my @pars = | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 'begin', 1, 'b', | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 'begin of section (above all other criteria)' | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ,'end', -1, 'e', | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 'end of section (when negative: until end; above all other criteria)' | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ,'reduce', '1', 'r', | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 'Reduce row count by a certain factor: Only include every ...th one. A value of 2 means rows 1,3,5... , a value of 10 means rows 1,11,21... (from the input).' | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ,'justmatch',1,'j', | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 'if an expression to match is given, select what to print out: 0 means all matches including header, >0 means just the first n matches, <0 means all matches, but no header' | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | ,'verbose',0,'v', | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 'be verbose about things' | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ,'ranges', [], 'R', 'give multiple ranges (format: "from:to", either may' | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | . ' be omitted or set to negative to extend to beginning/end) inside' | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | . ' the main range given by begin and end indices, this does not' | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | . ' duplicate or rearrange data, just specifies an additional condition' | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | . ' to include a record or not, in input order' | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | return $class->SUPER::new | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | ({ | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | parconf => | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | info=>$infostring # default version | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # default author | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # default copyright | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | ,pardef => \@pars | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | ,pipemode => 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ,pipe_init => \&preinit | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ,pipe_begin => \&init | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ,pipe_header => \&process_header | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | ,pipe_data => \&process_data | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | sub preinit | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
preinit: called CC=9 SCAR=26.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | my $p = $self->{param}; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # Sanitation... | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | ▶ | $self->{reduce} = $p->{reduce} < 1 ? 1 : $p->{reduce}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 4 | $self->{match} = shift(@{$self->{argv}}); | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | $self->{matchfun} = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | $self->{matchnohead} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | if(defined $self->{match}) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | $self->{matchfun} = expression_function($self->{match}, $p->{verbose}); | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | return print STDERR "Error creating function for matching ($!).\n" unless defined $self->{matchfun}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | $self->{matchnohead} = $p->{justmatch} != 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | $self->{ranges} = []; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | if(@{$p->{ranges}}) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | for(@{$p->{ranges}}) | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return print STDERR "Bad range: $_\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | unless /^\s*([+\-]?\d*)\s*:\s*([+\-]?\d*)\s*$/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | push(@{$self->{ranges}}, [$1 ne '' ? $1 : -1, $2 ne '' ? $2 : -1]); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | sub init | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 4 | $self->new_txd(); | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | $self->{line} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | $self->{matched} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | sub process_header | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_header: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 8 | ▶ | $_[0] = '' if $self->{matchnohead}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # Check if given line index is inside the configured ranges, | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # including reduction. Everything that works on the line index alone. | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | sub _chosen_line() | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_chosen_line: called CC=10 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 16 | my $p = $self->{param}; | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | 16 | my $i = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | return 0 # Global range first. | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 16 | ▶ | unless($i >= $p->{begin} and ($p->{end} < 0 or $i <= $p->{end})); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$i >= $p->{"begin"} and $p->{"end"} < 0 || $i <= $p->{"end"}
$p->{"end"} < 0 or $i <= $p->{"end"}
MC/DC66%2/3
$i >= $p->{"begin"} and $p->{"end"} < 0 || $i <= $p->{"end"}
$i >= $p->{"begin"}
$p->{"end"} < 0
$i <= $p->{"end"}
Truth table75%3/4
A and B or C
A= $i >= $p->{"begin"}B= $p->{"end"} < 0C= $i <= $p->{"end"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | return 0 # Line reduction, the other simple test. | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 14 | ▶ | unless(($self->{line}-1) % $self->{reduce} == 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # At last, the more elaborate check of sub-ranges. | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 14 | ▶ | if(@{$self->{ranges}}) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | for(@{$self->{ranges}}) | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | return 1 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$i >= $_->[0] and $_->[1] < 0 || $i <= $_->[1]
$_->[1] < 0 or $i <= $_->[1]
MC/DC0%0/3
$i >= $_->[0] and $_->[1] < 0 || $i <= $_->[1]
$i >= $_->[0]
$_->[1] < 0
$i <= $_->[1]
Truth table0%0/4
A and B or C
A= $i >= $_->[0]B= $_->[1] < 0C= $i <= $_->[1]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | if($i >= $_->[0] and ($_->[1] < 0 or $i <= $_->[1])); | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | return 0; # There are ranges, but none contain this index. | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 14 | return 1; # No further sub-ranges. It's in. | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | sub process_data | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_data: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 16 | my $p = $self->{param}; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 16 | ++$self->{line}; | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | # Checks based on line number. | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 16 | ▶ | unless($self->_chosen_line($self->{line})) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 2 | $_[0] = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | # Additional check with match function. | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 14 | ▶ | if(defined $self->{matchfun}) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 12 | ▶ | if | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $p->{"justmatch"} > 0 && !($self->{"matched"} < $p->{"justmatch"}) and $self->{"matchfun"}->([$self->{"txd"}->line_data($_[0])])
$p->{'justmatch'} > 0 && !($self->{'matched'} < $p->{'justmatch'})
MC/DC100%3/3
not $p->{"justmatch"} > 0 && !($self->{"matched"} < $p->{"justmatch"}) and $self->{"matchfun"}->([$self->{"txd"}->line_data($_[0])])
$p->{'justmatch'} > 0
!($self->{'matched'} < $p->{'justmatch'})
$self->{"matchfun"}->([$self->{"txd"}->line_data($_[0])])
Truth table100%5/5
not(A && not(B)) and C
A= $p->{'justmatch'} > 0B= !($self->{'matched'} < $p->{'justmatch'})C= $self->{"matchfun"}->([$self->{"txd"}->line_data($_[0])])
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | not($p->{justmatch} > 0 and not $self->{matched} < $p->{justmatch}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | and $self->{matchfun}->([$self->{txd}->line_data($_[0])]) | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 4 | ){ ++$self->{matched}; } | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | 8 | else{ $_[0] = ''; } | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||