| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# numbound: Find boundary numbers in files or STDIN. |
| 4 |
# |
| 5 |
# Copyright (C) 2002-2004 Suso Banderas |
| 6 |
|
| 7 |
# This program is free software; you can redistribute it and/or |
| 8 |
# modify it under the terms of the GNU General Public License |
| 9 |
# as published by the Free Software Foundation; either version 2 |
| 10 |
# of the License, or (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program; if not, write to the Free Software |
| 19 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 |
# |
| 21 |
# You may contact the author at <[email protected]>. |
| 22 |
|
| 23 |
####################### |
| 24 |
# VARIABLES AND SETUP # |
| 25 |
####################### |
| 26 |
|
| 27 |
use Getopt::Std; |
| 28 |
use strict; |
| 29 |
use vars qw/ %opts $verbose /; |
| 30 |
|
| 31 |
my ($file, @number_array); |
| 32 |
|
| 33 |
getopts('dhlV', \%opts); |
| 34 |
|
| 35 |
if ($opts{'h'}) { |
| 36 |
&help(); |
| 37 |
exit(0); |
| 38 |
} |
| 39 |
|
| 40 |
if ($opts{'d'}) { |
| 41 |
$verbose = 3; |
| 42 |
print STDERR "Debug mode\n"; |
| 43 |
} elsif ($opts{'V'}) { |
| 44 |
$verbose = 2; |
| 45 |
print STDERR "Verbose mode\n"; |
| 46 |
} elsif ($opts{'q'}) { |
| 47 |
$verbose = 0; # Nothing except the final answer |
| 48 |
} else { |
| 49 |
$verbose = 1; # Normal warnings and such. |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
################ |
| 54 |
# MAIN PROGRAM # |
| 55 |
################ |
| 56 |
|
| 57 |
# For file args |
| 58 |
if (@ARGV) { |
| 59 |
foreach $file (@ARGV) { |
| 60 |
print STDERR "Reading from file $file.\n" if ($verbose >= 2); |
| 61 |
open (ARGFILE, "$file") && |
| 62 |
process_filehandle(\*ARGFILE, \@number_array) || |
| 63 |
$verbose && warn "Couldn't open file $file for reading: $!\n"; |
| 64 |
close (ARGFILE); |
| 65 |
} |
| 66 |
# For STDIN |
| 67 |
} else { |
| 68 |
print STDERR "Reading from STDIN.\n" if ($verbose >= 2); |
| 69 |
process_filehandle(\*STDIN, \@number_array); |
| 70 |
} |
| 71 |
|
| 72 |
my $final_number; |
| 73 |
if ($opts{'l'}) { |
| 74 |
$final_number = find_lower_bound(\@number_array); |
| 75 |
} else { |
| 76 |
$final_number = find_upper_bound(\@number_array); |
| 77 |
} |
| 78 |
|
| 79 |
print "$final_number\n"; |
| 80 |
|
| 81 |
exit(0); |
| 82 |
|
| 83 |
############### |
| 84 |
# SUBROUTINES # |
| 85 |
############### |
| 86 |
|
| 87 |
sub help { |
| 88 |
print <<"EOF"; |
| 89 |
------------------------------------------------------------------------------- |
| 90 |
numbound : Find boundary numbers in files or STDIN. By default, find the maximum. |
| 91 |
------------------------------------------------------------------------------- |
| 92 |
Usage: |
| 93 |
|
| 94 |
numbound [options] <file> |
| 95 |
| numbound [options] |
| 96 |
numbound [options] |
| 97 |
|
| 98 |
Options: |
| 99 |
-l -- Return the lower bound number in the set (the minimum number) |
| 100 |
|
| 101 |
-d Debug. For developers only. |
| 102 |
-h Help: You're looking at it. |
| 103 |
-V Increase verbosity. |
| 104 |
EOF |
| 105 |
} |
| 106 |
|
| 107 |
sub process_filehandle { |
| 108 |
my $filehandle = shift; |
| 109 |
my $number_array_ref = shift; |
| 110 |
|
| 111 |
while (<$filehandle>) { |
| 112 |
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) { |
| 113 |
print STDERR "found number: $1\n" if ($verbose >= 3); |
| 114 |
push(@$number_array_ref, $1); |
| 115 |
} |
| 116 |
} |
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
sub find_upper_bound { |
| 121 |
my $number_array_ref = shift; |
| 122 |
my $highest_number = @$number_array_ref[0]; |
| 123 |
my $number; |
| 124 |
|
| 125 |
foreach $number (@$number_array_ref) { |
| 126 |
if ($number > $highest_number) { |
| 127 |
$highest_number = $number; |
| 128 |
} |
| 129 |
} |
| 130 |
return $highest_number; |
| 131 |
} |
| 132 |
|
| 133 |
sub find_lower_bound { |
| 134 |
my $number_array_ref = shift; |
| 135 |
my $lowest_number = @$number_array_ref[0]; |
| 136 |
my $number; |
| 137 |
|
| 138 |
foreach $number (@$number_array_ref) { |
| 139 |
if ($number < $lowest_number) { |
| 140 |
$lowest_number = $number; |
| 141 |
} |
| 142 |
} |
| 143 |
return $lowest_number; |
| 144 |
} |
| 145 |
|
| 146 |
# Lay down some of that perl pod action. |
| 147 |
=pod |
| 148 |
|
| 149 |
=head1 NAME |
| 150 |
|
| 151 |
numbound - Find boundary numbers in files or STDIN. |
| 152 |
|
| 153 |
=head1 SYNOPSIS |
| 154 |
|
| 155 |
B<numbound> [-dhlV] <FILE> |
| 156 |
|
| 157 |
| B<numbound> [-dhlV] (Input on STDIN from pipeline.) |
| 158 |
|
| 159 |
B<numbound> [-dhlV] (Input on STDIN. Use Ctrl-D to stop.) |
| 160 |
|
| 161 |
=head1 DESCRIPTION |
| 162 |
|
| 163 |
B<numbound> |
| 164 |
will find boundary numbers (minimum and maximum) in files or STDIN. By default |
| 165 |
it will find the upper bound in the set of numbers (the maximum number) in the |
| 166 |
files or on STDIN. You can use the -l option for finding the lower bound (minimum |
| 167 |
number). |
| 168 |
|
| 169 |
=head1 OPTIONS |
| 170 |
|
| 171 |
-l -- Return the lower bound number in the set (the minimum number) |
| 172 |
|
| 173 |
-h Help: You're looking at it. |
| 174 |
-V Increase verbosity. |
| 175 |
-d Debug mode. For developers |
| 176 |
|
| 177 |
=head1 BUGS |
| 178 |
|
| 179 |
B<numbound> currently will only gather the first number on each line instead of |
| 180 |
all the numbers on the lines. |
| 181 |
|
| 182 |
=head1 SEE ALSO |
| 183 |
|
| 184 |
numaverage(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numrange(1), numround(1) |
| 185 |
|
| 186 |
=head1 COPYRIGHT |
| 187 |
|
| 188 |
numbound is part of the num-utils package, which is copyrighted by |
| 189 |
Suso Banderas and released under the GPL license. Please read |
| 190 |
the COPYING and LICENSE files that came with the num-utils package |
| 191 |
|
| 192 |
Developers can read the GOALS file and contact me about providing |
| 193 |
submitions or help for the project. |
| 194 |
|
| 195 |
=head1 MORE INFO |
| 196 |
|
| 197 |
More info on numbound can be found at: |
| 198 |
|
| 199 |
=over 1 |
| 200 |
|
| 201 |
=item B<http://suso.suso.org/programs/num-utils/> |
| 202 |
|
| 203 |
=back |
| 204 |
|
| 205 |
=cut |
| 206 |
|