-
Notifications
You must be signed in to change notification settings - Fork 1
/
numprocess
executable file
·278 lines (206 loc) · 7.47 KB
/
numprocess
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/perl -w
# numprocess: This program mutates numbers as it encounters them.
#
# Copyright (C) 2002-2004 Suso Banderas
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# You may contact the author at <suso@suso.org>.
#######################
# VARIABLES AND SETUP #
#######################
#use bignum;
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
my ($file);
getopts('hdV', \%opts);
if ($opts{'h'}) {
&help();
exit(0);
}
if ($opts{'d'}) {
$verbose = 3;
print STDERR "Debug mode\n";
} elsif ($opts{'V'}) {
$verbose = 2;
print STDERR "Verbose mode\n";
} elsif ($opts{'q'}) {
$verbose = 0; # Nothing except the final answer
} else {
$verbose = 1; # Normal warnings and such.
}
my $expression = shift;
$expression =~ s/^\/(.*)\/$/$1/;
################
# MAIN PROGRAM #
################
# For file args
if (@ARGV) {
foreach $file (@ARGV) {
print STDERR "Reading from file $file.\n" if ($verbose >= 2);
open (ARGFILE, "$file") && process_filehandle(\*ARGFILE) ||
$verbose && warn "Couldn't open file $file for reading: $!\n";
close (ARGFILE);
}
# For STDIN
} else {
print STDERR "Reading from STDIN.\n" if ($verbose >= 2);
process_filehandle(\*STDIN);
}
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
-----------------------------------------------------------------
numprocess: This program mutates numbers as it encounters them.
-----------------------------------------------------------------
Usage:
numprocess [options] /<expression>/ <file>
| numprocess [options] /<expression>/
numprocess [options] /<expression>/
Options:
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
Expressions:
Put a list of operations to do on each number in the expression area.
Possible expressions:
+ Addition
- Subtraction
* Multiplication
% Division
^ Power function
modN Modulo operation against N - ex. mod26
sqrt Square Root
sin Sine function
cos Cosine function
Constants and keywords that can be used
pi 3.141592654
e 2.718281828
See the man page for more details.
EOF
}
sub process_filehandle {
my $filehandle = shift;
while (<$filehandle>) {
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
print STDERR "found number: $1\n" if ($verbose >= 3);
print process_number($1) . "\n";
}
}
return 1;
}
sub process_number {
my $number = shift;
my $new_number = $number;
my $operation;
my $match_expression = "-?[0-9]*\.?[0-9]*";
foreach $operation (split(/,/, $expression)) {
$operation =~ s/pi$/3.141592654/i;
$operation =~ s/e$/2.718281828/i;
print "operation is $operation\n" if ($verbose >= 3);
if ($operation =~ /^\+($match_expression)$/) {
$new_number = $new_number + $1;
} elsif ($operation =~ /^\-($match_expression)$/) {
$new_number = $new_number - $1;
} elsif ($operation =~ /^\^($match_expression)$/) {
$new_number = ${new_number}**${1};
} elsif ($operation =~ /^\*($match_expression)$/) {
# This has issues of course if you have a directory in your root directory that ends in 1, 2, 3 etc.
# On my system I have a /home2 and *2 matches that, I'll have to fix this
# so that quotes can be used instead of //. Not sure why I choose to use //
$new_number = $new_number * $1;
} elsif ($operation =~ /^\%($match_expression)$/) {
$new_number = $new_number / $1;
} elsif ($operation =~ /^mod($match_expression)$/) {
$new_number = $new_number % $1;
} elsif ($operation =~ /^sqrt$/) {
if ($new_number < 0) { # let's do something nice for negative sqrts.
$new_number *= -1;
$new_number = sqrt($new_number);
$new_number .= "i"; # for imaginary. Just like in High School Algebra.
} else {
$new_number = sqrt($new_number);
}
} else {
print "Encountered invalid expression $operation in $expression.\n" if ($verbose >= 2);
return $number;
}
}
return $new_number;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
numprocess - This program mutates numbers as it encounters them.
=head1 SYNOPSIS
B<numprocess> [-dhV] /<expression>/ [FILE or STDIN]
| B<numprocess> [-dhV] /<expression>/ (Input on STDIN from pipeline.)
B<numprocess> [-dhV] /<expression>/ (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B<numprocess> is a program that is part of the numeric utilities package. B<numprocess>
will take as one argument, a list of operations to be performed on numbers that it
encounters. It will perform those operations on each number and return the result in place
of the original number.
=head1 USAGE EXAMPLES
Add 1 to all numbers.
$ numprocess /+1/ file1
Convert all numbers from miles to kilometers. Multiply by 8 and divide by 5.
$ cat file1 | numprocess /*8,%5/
Convert from Celsius to Fahrenheit degrees. Multiply by 9, divide by 5 and add 32.
$ numprocess /*9,%5,+32/ temperatures
Find the area of each circle from the given radius.
$ numprocess /^2,*pi/ radii
=head1 KEYWORDS AND OPERATORS
For operators, the modifying number goes directly after the operator, with
the exception of functions like sqrt, sin, cos, etc.
+ Addition
- Subtraction
* Multiplication
% Division
^ Power function
modN Modulo operation against N - ex. mod26
sqrt Square Root (*)
sin Sine function
cos Cosine function
Constants and keywords that can be used
pi 3.141592654
e 2.718281828
(*) When using the sqrt operation on negative numbers, it will take the
absolute value of the number, sqrt it and then tack an i on the end
of the result to signify that the resulting number is imaginary.
=head1 OPTIONS
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
=head1 SEE ALSO
average(1), bound(1), interval(1), normalize(1), numgrep(1), numsum(1), random(1), range(1), round(1)
=head1 BUGS
There is currently no way to take the number found in the text stream and
use it as the numerator instead of the denominator of a division operation.
=head1 COPYRIGHT
numprocess is part of the num-utils package, which is copyrighted by
Suso Banderas and released under the GPL license. Please read
the COPYING and LICENSE files that came with the num-utils package
Developers can read the GOALS file and contact me about providing
submissions or help for the project.
=head1 MORE INFO
More info on numprocess can be found at:
=over 1
=item B<http://suso.suso.org/programs/num-utils/>
=back
=cut