| 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 |
$Getopt::Std::STANDARD_HELP_VERSION = 1; |
| 31 |
|
| 32 |
my ($file, @number_array); |
| 33 |
|
| 34 |
getopts('dhlV', \%opts); |
| 35 |
|
| 36 |
if ($opts{'h'}) { |
| 37 |
&help(); |
| 38 |
exit(0); |
| 39 |
} |
| 40 |
|
| 41 |
if ($opts{'d'}) { |
| 42 |
$verbose = 3; |
| 43 |
print STDERR "Debug mode\n"; |
| 44 |
} elsif ($opts{'V'}) { |
| 45 |
$verbose = 2; |
| 46 |
print STDERR "Verbose mode\n"; |
| 47 |
} elsif ($opts{'q'}) { |
| 48 |
$verbose = 0; # Nothing except the final answer |
| 49 |
} else { |
| 50 |
$verbose = 1; # Normal warnings and such. |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
################ |
| 55 |
# MAIN PROGRAM # |
| 56 |
################ |
| 57 |
|
| 58 |
# For file args |
| 59 |
if (@ARGV) { |
| 60 |
foreach $file (@ARGV) { |
| 61 |
print STDERR "Reading from file $file.\n" if ($verbose >= 2); |
| 62 |
open (ARGFILE, "$file") && |
| 63 |
process_filehandle(\*ARGFILE, \@number_array) || |
| 64 |
$verbose && warn "Couldn't open file $file for reading: $!\n"; |
| 65 |
close (ARGFILE); |
| 66 |
} |
| 67 |
# For STDIN |
| 68 |
} else { |
| 69 |
print STDERR "Reading from STDIN.\n" if ($verbose >= 2); |
| 70 |
process_filehandle(\*STDIN, \@number_array); |
| 71 |
} |
| 72 |
|
| 73 |
my $final_number; |
| 74 |
if ($opts{'l'}) { |
| 75 |
$final_number = find_lower_bound(\@number_array); |
| 76 |
} else { |
| 77 |
$final_number = find_upper_bound(\@number_array); |
| 78 |
} |
| 79 |
|
| 80 |
print "$final_number\n"; |
| 81 |
|
| 82 |
exit(0); |
| 83 |
|
| 84 |
############### |
| 85 |
# SUBROUTINES # |
| 86 |
############### |
| 87 |
|
| 88 |
sub help { |
| 89 |
print <<"EOF"; |
| 90 |
------------------------------------------------------------------------------- |
| 91 |
numbound : Find boundary numbers in files or STDIN. By default, find the maximum. |
| 92 |
------------------------------------------------------------------------------- |
| 93 |
Usage: |
| 94 |
|
| 95 |
numbound [options] <file> |
| 96 |
| numbound [options] |
| 97 |
numbound [options] |
| 98 |
|
| 99 |
Options: |
| 100 |
-l -- Return the lower bound number in the set (the minimum number) |
| 101 |
|
| 102 |
-d Debug. For developers only. |
| 103 |
-h Help: You're looking at it. |
| 104 |
-V Increase verbosity. |
| 105 |
EOF |
| 106 |
} |
| 107 |
|
| 108 |
sub HELP_MESSAGE{ |
| 109 |
&help(); |
| 110 |
} |
| 111 |
|
| 112 |
sub process_filehandle { |
| 113 |
my $filehandle = shift; |
| 114 |
my $number_array_ref = shift; |
| 115 |
|
| 116 |
while (<$filehandle>) { |
| 117 |
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) { |
| 118 |
print STDERR "found number: $1\n" if ($verbose >= 3); |
| 119 |
push(@$number_array_ref, $1); |
| 120 |
} |
| 121 |
} |
| 122 |
return 1; |
| 123 |
} |
| 124 |
|
| 125 |
sub find_upper_bound { |
| 126 |
my $number_array_ref = shift; |
| 127 |
my $highest_number = @$number_array_ref[0]; |
| 128 |
my $number; |
| 129 |
|
| 130 |
foreach $number (@$number_array_ref) { |
| 131 |
if ($number > $highest_number) { |
| 132 |
$highest_number = $number; |
| 133 |
} |
| 134 |
} |
| 135 |
return $highest_number; |
| 136 |
} |
| 137 |
|
| 138 |
sub find_lower_bound { |
| 139 |
my $number_array_ref = shift; |
| 140 |
my $lowest_number = @$number_array_ref[0]; |
| 141 |
my $number; |
| 142 |
|
| 143 |
foreach $number (@$number_array_ref) { |
| 144 |
if ($number < $lowest_number) { |
| 145 |
$lowest_number = $number; |
| 146 |
} |
| 147 |
} |
| 148 |
return $lowest_number; |
| 149 |
} |
| 150 |
|
| 151 |
# Lay down some of that perl pod action. |
| 152 |
=pod |
| 153 |
|
| 154 |
=head1 NAME |
| 155 |
|
| 156 |
numbound - Find boundary numbers in files or STDIN. |
| 157 |
|
| 158 |
=head1 SYNOPSIS |
| 159 |
|
| 160 |
B<numbound> [-dhlV] <FILE> |
| 161 |
|
| 162 |
| B<numbound> [-dhlV] (Input on STDIN from pipeline.) |
| 163 |
|
| 164 |
B<numbound> [-dhlV] (Input on STDIN. Use Ctrl-D to stop.) |
| 165 |
|
| 166 |
=head1 DESCRIPTION |
| 167 |
|
| 168 |
B<numbound> |
| 169 |
will find boundary numbers (minimum and maximum) in files or STDIN. By default |
| 170 |
it will find the upper bound in the set of numbers (the maximum number) in the |
| 171 |
files or on STDIN. You can use the -l option for finding the lower bound (minimum |
| 172 |
number). |
| 173 |
|
| 174 |
=head1 OPTIONS |
| 175 |
|
| 176 |
-l -- Return the lower bound number in the set (the minimum number) |
| 177 |
|
| 178 |
-h Help: You're looking at it. |
| 179 |
-V Increase verbosity. |
| 180 |
-d Debug mode. For developers |
| 181 |
|
| 182 |
=head1 BUGS |
| 183 |
|
| 184 |
B<numbound> currently will only gather the first number on each line instead of |
| 185 |
all the numbers on the lines. |
| 186 |
|
| 187 |
=head1 SEE ALSO |
| 188 |
|
| 189 |
numaverage(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numrange(1), numround(1) |
| 190 |
|
| 191 |
=head1 COPYRIGHT |
| 192 |
|
| 193 |
numbound is part of the num-utils package, which is copyrighted by |
| 194 |
Suso Banderas and released under the GPL license. Please read |
| 195 |
the COPYING and LICENSE files that came with the num-utils package |
| 196 |
|
| 197 |
Developers can read the GOALS file and contact me about providing |
| 198 |
submitions or help for the project. |
| 199 |
|
| 200 |
=head1 MORE INFO |
| 201 |
|
| 202 |
More info on numbound can be found at: |
| 203 |
|
| 204 |
=over 1 |
| 205 |
|
| 206 |
=item B<http://suso.suso.org/programs/num-utils/> |
| 207 |
|
| 208 |
=back |
| 209 |
|
| 210 |
=cut |
| 211 |
|