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