| 1 | package Text::NumericData::App::txdextrema; | ||
| 2 | |||
| 3 | use Text::NumericData::App; | ||
| 4 | |||
| 5 | use strict; | ||
| 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 | our $VERSION = '1'; | ||
| 11 | $VERSION = eval $VERSION; | ||
| 12 | |||
| 13 | my $infostring = 'find maxima/minima in textual data files | ||
| 14 | |||
| 15 | Usage: | ||
| 16 | txdextrema [n] < data.dat | ||
| 17 | |||
| 18 | The column we are interested in is simply given as a number (starting at 1) after any options; default is the second one (making sense for x-y data) or the only present one. | ||
| 19 | By default the occuring extremal value is printed, but you can choose to print all rows where it occurs.'; | ||
| 20 | |||
| 21 | our @ISA = ('Text::NumericData::App'); | ||
| 22 | |||
| 23 | sub new | ||
| 24 | { | ||
| 25 | my $class = shift; | ||
| 26 | my @pars = | ||
| 27 | ( | ||
| 28 | 'minima',0,'m', | ||
| 29 | 'look for minima (=0 means looking for maxima)' | ||
| 30 | , 'print',0,'p', | ||
| 31 | 'print the concerned data sets (otherwise only printing the found value)' | ||
| 32 | ); | ||
| 33 | |||
| 34 | return $class->SUPER::new | ||
| 35 | ({ | ||
| 36 | parconf => | ||
| 37 | { | ||
| 38 | info=>$infostring # default version | ||
| 39 | # default author | ||
| 40 | # default copyright | ||
| 41 | } | ||
| 42 | ,pardef => \@pars | ||
| 43 | ,pipemode => 1 | ||
| 44 | ,pipe_init => \&preinit | ||
| 45 | ,pipe_begin => \&init | ||
| 46 | ,pipe_header => \&ignore | ||
| 47 | ,pipe_data => \&process_data | ||
| 48 | ,pipe_end => \&result | ||
| 49 | }); | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub preinit | ||
| 54 | { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | $self->{col} = shift @{$self->{argv}}; | ||
| 58 | $self->{guesscol} = not defined $self->{col}; | ||
| 59 | unless($self->{guesscol} or $self->{col} >= 1) | ||
| 60 | { | ||
| 61 | print STDERR "Need column >= 1!\n"; | ||
| 62 | return -1; | ||
| 63 | } | ||
| 64 | --$self->{col}; | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub init | ||
| 70 | { | ||
| 71 | my $self = shift; | ||
| 72 | $self->new_txd(); | ||
| 73 | $self->{ext} = undef; | ||
| 74 | @{$self->{data}} = (); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub ignore | ||
| 78 | { | ||
| 79 | my $self = shift; | ||
| 80 | $_[0] = ''; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub process_data | ||
| 84 | { | ||
| 85 | my $self = shift; | ||
| 86 | my $param = $self->{param}; | ||
| 87 | my $data = $self->{txd}->line_data($_[0]); | ||
| 88 | $_[0] = ''; | ||
| 89 | return unless defined $data->[$self->{col}]; | ||
| 90 | |||
| 91 | if($self->{guesscol} and not defined $self->{col}) | ||
| 92 | { | ||
| 93 | $self->{col} = $#{$data} < 1 ? 0 : 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | if( not defined $self->{ext} or $param->{minima} | ||
| 97 | ? $data->[$self->{col}] < $self->{ext} | ||
| 98 | : $data->[$self->{col}] > $self->{ext} ) | ||
| 99 | { | ||
| 100 | $self->{ext} = $data->[$self->{col}]; | ||
| 101 | @{$self->{data}} = ($data) if $param->{print}; | ||
| 102 | } | ||
| 103 | elsif($param->{print} and $data->[$self->{col}] == $self->{ext}) | ||
| 104 | { | ||
| 105 | push(@{$self->{data}}, $data); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | sub result | ||
| 110 | { | ||
| 111 | my $self = shift; | ||
| 112 | my $param = $self->{param}; | ||
| 113 | |||
| 114 | return unless defined $self->{ext}; | ||
| 115 | |||
| 116 | if($param->{print}) | ||
| 117 | { | ||
| 118 | foreach my $d (@{$self->{data}}) | ||
| 119 | { | ||
| 120 | $_[0] .= ${$self->{txd}->data_line($d)}; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | else | ||
| 124 | { | ||
| 125 | $_[0] = $self->{ext}.$self->{txd}->{config}{lineend}; | ||
| 126 | } | ||
| 127 | } |