num-utils-0.5/0040775000076400007640000000000010147276463012107 5ustar susosusonum-utils-0.5/CHANGELOG0100664000076400007640000000541410147276245013320 0ustar susosuso
Nov. 19th, 2004 04:53 GMT
----------------------------
Fifth Beta Release v0.5
- Cleaned up a few bits of code in numsum.
- Adjusted copyright years to 2004.
Aug. 28th, 2004 06:40 GMT
----------------------------
- Added -c,-r,-x,-y options to numsum to allow it
to sum columns and rows and specify which rows and columns you want.
* Special thanks to the folks on #perl on irc.freenode.org for helping
me out with the regex for this one.
- Added - number hack to numsum to quickly specify the column number
that you want to sum. For now it only does columns 1 through 9 because
I need to figure out how to get past Getopt::Std to get the whole number
specified. Since this is such a hack, I might just leave it as is or drop it.
Aug. 25th, 2004 19:56 GMT
----------------------------
- Made some changes to the GOALS file in regards to what options are being planned.
Sept. 23rd, 2003 22:23 GMT
----------------------------
Fourth Beta Release v0.4
- Added -R option to normalize
- updated utils to include man page reference to normalize
- updated range program to include man page reference to seq(1)
- Added feedback directory and files to CVS.
Sept. 22nd, 2003 22:24 GMT
----------------------------
- Added normalize program and documentation.
Sept. 21st, 2003 20:51 GMT
---------------------------
- Added median function and -M option
- Added -l option for using the lower median number on even sets.
Sept. 19th, 2003 15:50 GMT
----------------------------
- Added find_mode subroutine to average program and respective documentation.
June 9th, 2003 14:48 GMT
---------------------------
Third Beta Release v0.3
- Added -q option to interval and updated documentation.
June 3rd, 2003 23:26 GMT
---------------------------
- Added interval program with initial functionality
- Changed man page listing on other programs to include interval
- Updated GOALS file to include 'interval' goals and also added the -t
option to numsum's goals.
- Updated spec file with interval file entries.
- Updated README file.
May 29th, 2003 20:12 GMT
--------------------------
Second Beta Release v0.2
- Fixed formatting in GOALS document.
- Added some of the features submitted by areiner@tph.tuwien.ac.at
to the GOALS file.
- Added helpful information for the 'bound' program to explain that the
default is to find the maximum number.
- Added code to 'numgrep' to handle factors and multiples.
- Added expression info and examples to 'numgrep' man page/pod and help output.
- Added code and documentation for -e (exclude) option for 'range'.
- Fixed a few things in the README.
- Made the sqrt function in numprocess process negative numbers as imaginary.
June 17th, 2002 13:00 GMT
--------------------------
Initial Beta Release v0.1
num-utils-0.5/COPYING0100664000076400007640000004312707401755122013137 0ustar susosuso GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C) 19yy
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
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) 19yy name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.
num-utils-0.5/GOALS0100664000076400007640000004657310147276245012711 0ustar susosuso
num-utils - Goals for this project
-----------------------------------
(NOTE: This document does not represent a currrent feature set, please read
the README file and man pages instead.)
This document outlines certain criteria that the num-utils should eventually
meet. It outlines a general interface that each utility should follow and
guides the project to bring the num-utils to version 1.0
The initial releases of the num-utils will be small and much less featured
than what is shown below. I originally wrote the num-utils with many of
the features shown below, but everything was rather unorganized and some of
the programs simply processed the data incorrectly. I decided that I would
go back to the beginning, write down all the features that I would like to
have and think are useful, and create a development cycle. I'll release
each version to the public for review, comments and contribution. Anyone who
is interested in helping out in any way, please contact me at suso@suso.org.
Any contributions are welcome.
I've put an 'X' at the begining of the line for features that have been
completeled.
-= All numeric utilities =-
1. All num utils should process data on either STDIN or from a file or files
specified from command line arguments.
Examples of STDIN usage:
cat file | num-util
num-util < file
num-util
[Program will wait for data on STDIN until Ctrl-D is pressed]
Examples of file argument usage:
num-util file
num-util file1 file2 file3 ... fileN
num-util -[options] file1 file2 file3
2. All num utils should use the following options that are standard among
all the utilities:
X -h -- provide helpful usage information
X -V -- Be verbose. Send verbose information to STDERR.
X -d -- Debug information for developers. This implies verbose and more.
-q -- Quiet mode, Don't print out any warnings about file read errors.
-i -- Output numeric values as their integer only.
-I -- Output numeric values as their decimal portion only.
-a -- Don't treat '-' characters as negative signs.
Think of -a as 'absolute value'.
-d -- Don't treat the '.' character as a decimal point.
-N -- Don't process complete numbers, process each numeric character
individually.
-C -- Commify numbers in output. Instead of printing out '1000000', it
should print out '1,000,000'.
-b
-- Use the following base when dealing with numbers.
-- -- Don't process any more arguments as options.
3. All num utils should provide user documentation in the form of man pages.
- While the programs are written in perl I plan on using POD information in
the programs themselves and then that documentation can be exported to man
pages, info docs or HTML format.
- Eventually I would like to write the documentation in SGML so that it can
be easily converted into many different formats.
4. All num utils should provide summarized help with the -h option simular to
the following:
----------------------------------------------
numutil: process numbers in such a way......
----------------------------------------------
Usage: numutil [options] [file args]
STDIN | numutil [options]
numutil [options] < file
Options:
-h Help: You're looking at it.
-V Increase verbosity.
-d Don't treat the '.' character as a decimal point.
5. All numeric utilities should abstract their functionality as much as
possible so that a numeric processing module can be written to make things
consistent among all numeric utility programs.
6. The num-utils set of utilities should be available in the following forms
for download:
o tar format, both gz and bz2
o rpm package
o gentoo ebuild tree
o deb package
7. The Makefile should be setup to be able to create the different package
formats listed above as well as the man pages from the perldoc information
in each utility's source code.
8. Numeric expressions (regular expressions, plus operations for numbers)
.. -- range operator
i -- increment operator
f -- factor operator
m -- multiple operator
, -- expression seperation separator
[] -- character grouping
+ -- quantifier (1 or more)
* -- quantifier (0 or more)
? -- Match the preceding character 0 or 1 times.
{} -- for matching specific number of times.
-- Some goals sent in by areiner@tph.tuwien.ac.at (thanks) --
- Support for locale changes between . and , usage. In other parts of
the world they use a period in the place of a decimal point
(ie. 1.000.000,50 means one million and 5 tenths).
- Some of the utilities should be able to recurse through subdirectories,
where appropriate.
- Support for output and input of numbers in scientific noation.
(ie. 1.3e-7 for 0.0000013)
- A utility for sorting numbers in mixed notations. For instance, it
is hard to sort numbers that are in scientific notation or commified
along side ones that aren't. Maybe this is a call for a numsort utility.
-= Individual program goals =-
-- numsum --
This program adds up all numbers encountered. In it's basic operation, it
will simply add up all the numbers that it encounters. The numbers can be one
per line, multiple per line separated by space or multiple numbers separated
by anything.
Eventually, it would be nice if numsum could do things like sum up individual
rows or columns of input separately. There might be other types of summing
functions that would be useful when dealing with textual input.
Examples
$ cat numbers
1
2
3
4
$ cat numbers | numsum
10
$ numsum numbers
10
Advanced Examples
$ cat columns
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
$ cat columns | numsum -c
15 40 65 90 115
$ cat columns | numsum -r (add up the rows)
55
60
65
70
75
$
Usage options (in addition to the standard options)
-a -- Add all numbers in the file, not just the first ones found on each
line.
X -c -- Treat each line as a set of columns separated by white space, or a
string if the -s option is used. Sum up the values in each column
and print out the result of each column seperated by the seperation
character. This is shown above in the advanced examples section.
X -s [for columns]
-- Use as the separator between each column. This is allowed
to be more than one character and possibly even a number.
X -r -- Treat each line (by default) as a row of numbers to sum up. The
results of the sums of each row will be printed on seperate lines.
-s [for rows]
-- When used with the -r flag, this will specify the seperator for
rows. By default it is the new line character. It could be a
character, set of characters or even a number.
-t
-- When used, it will add up every th number and at the end
print out rows showing the sums of each th number. This
would be useful for example if you had data from each day of the
month and you wanted to sum up the date for each week day.
So you might do something like this
$ cat monthly-data | numsum -t 7
Options that might be included eventually.
X -x -- Where is some number. This would be a shortcut for adding up
all the numbers in the th column of the input. By default, the
columns would be determined by white space, but could also be
determined by the -s flag. This must be used with the -c or -r flag.
So you can do something like:
$ numsum -c -x 10 access_log
To get the total bytes transfered in an access_log.
Maybe this could also be able to handle comma seperated values, so
1,5,10 would sum up the 1st, 5th and 10th columns.
-- numgrep --
This program is the numeric equivilent of the unix grep utility. numgrep
will search textual input for numbers matching the expression specified from
the command line. The main power of numgrep is in being able to search for
ranges of numbers. Such as searching for all numbers between 1 and 100.
Normal unix grep and regular expressions would not allow you to do this simple
task, but with numgrep's numeric matching expressions it is possible to match
numbers in ways not previously possible.
A few examples of numgrep's usage:
X o Search for all numbers between 1 and 100 in the file data.txt.
numgrep /1..100/ data.txt
X o search for numbers between 1 and 37,even numbers between 50 and 58 as
well as the numbers 79, 86 and 94.
numgrep /1..37,5[2468],79,86,94/ data.txt
X o search for numbers from -10 to 10.
numgrep /-10..10/ data.txt
X o search for numbers that are multiples of 7
numgrep /m7/ data.txt
o search for numbers that are multiples of 7, 12 and 22
numgrep /m7m12m22/ data.txt
o search for numbers that are factors of 1024 and multiples of 12 or numbers
that are factors of 2333 and multiples of 9
numgrep /f1024m12,f2333m9/ data.txt
o seach for numbers that are in the set 1, 4, 7, 10, 13 and 16
numgrep /1..16i3/ data.txt
Usage options (in addition to the standard options)
-l -- Instead of keeping the numbers in their textual context, print
them out one number per line.
-R -- Inverse the sense of matching. To match non-matching numbers
-r -- Recurse subdirectories.
-p
-- Obtain the patterns from file
-f -- Suppress normal output and just print the names of the files
that contain a match one per line.
-F -- Suppress normal output and print the names of the files for which
no output would have been printed.
-n -- Print the line number in front of each line that contains a match.
-c -- Don't save non-numeric context. This will cause numgrep to dump all
non-matching/non-numeric values around the numbers that match.
Eventual options
-A [n] -- Print [n] lines of context out after the matching line.
The default is 2.
-B [n] -- Print [n] lines of context out before the matching line.
The default is 2.
Feature submitted by areiner@tph.tuwien.ac.at:
- Intervals: I may be biased due to my work, but usually when you
determine a quantity with only finite precision, people write
something like 1.234 +/- 0.056 or 1.234(56) (the last form is more
common, at least in physics journals; the interpretation is that the
digits in brackets are taken to line up with the last digit shown,
left-padded with zeroes and a decimal point in the correct place). The
natural representation of both of these is as intervals, i.e. as sets
of two numbers representing lower and upper bounds. Thus, the given
number should be turned into something like 1.178..1.290, and there
should be a conversion function that turns it back into either of the
other two.
-- average --
This program finds the average of all numbers encountered. By default it will
find the mean average of all the numbers. Meaning (;-) that it will add up all
the values and divide that sum by the number of values encountered. It should
eventually offer more sophisticated calculations like finding the median and
mode values.
Examples of usage:
$ cat numbers
4
8
9
20
99
$ average numbers
28
Usage options:
X -m -- Print out the mode value of all the numbers entered. The mode is
the most frequently occuring value in the set.
X -M -- Print out the median of the set of numbers entered. The median is
the middle value all all numbers encountered. So if the numbers
88, 12, 2, 1, 9, 100 and 1000 are encountered, the median of that
set is 12. Illustrated:
1 2 9 12 88 100 1000
^^
X -l -- Use the lower number of the median on even counted sets.
-a -- average all numbers in the file, not just the first ones found on
each line.
-c -- Treat each line as a set of columns separated by white space, or a
string if the -s option is used. Average the values in each column
and print out the result of each column seperated by the seperation
character. This is shown above in the advanced examples section.
-s [for columns]
-- Use as the separator between each column. This is allowed
to be more than one character and possibly even a number.
-r -- Treat each line (by default) as a row of numbers to sum up. The
results of the averages of each row will be printed on seperate
lines.
-s [for rows]
-- When used with the -r flag, this will specify the seperator for
rows. By default it is the new line character. It could be a
character, set of characters or even a number.
Options that might be included eventually.
-n -- Where is some number. This would be a shortcut for averaging
all the numbers in the th column of the input. By default, the
columns would be determined by white space, but could also be
determined by the -s flag. This must be used with the -c or -r flags.
Usage options (in addition to the standard options)
-- normalize --
This program will distribute a group of numbers between 0 and 1 by default according
to their initial value. You can change the range using the -R option.
Usage options:
X -R -- This is for specifying a range to normalize for instead of 0..1
-- round --
This utility will round each number encountered up or down depending on it's
decimal value or it's relation to a number. It will probably also deal with
finding the floor or ceiling values of numbers. It should also be able to
round to factors of certain numbers.
Options:
X -n -- Round to the nearest factor of . Instead of just rounding all
decimal numbers, you can also round to a factor of any number. So
if you set to 1000 and you encounter the number 6777, it will
round that number to 7000. If you set to 3 and encounter the
number 7, it will round it to 6.
X -c -- Find the ceiling of each number encountered. Round up.
X -f -- Find the floor of each number. Round down.
-- range --
Print out a range of numbers for use in for loops and such.
o Do zero or character padding with the -p option. So 1 becomes 001 if the
range has an upper limit of 3 digits.
o Accept ranges in the following formats:
n1..n2 (ex. 1..100 ; All integers from 1 to 100)
n1..n2,n3..n4 (ex. 1..10,50..100 ; All integers from 1 to 10 and
from 50 to 100)
n1..n2i2 (ex. 2..20i2 ; All even numbers from 2 to 20)
(ex. 1..19i2 ; All odd numbers from 1 to 19)
(ex. 3..21i3 ; 3, 6, 9, 12, 15, 18, 21)
n1.d..n2.di0.1
(ex. 1.1..2.0i0.1 ; 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8,
1.9, 2.0)
Or any combination of the above.
Usage options (in addition to the standard options)
-p -- Put the following prefix before each number.
-s -- Put the following suffix after each number.
X -n -- Use as a character to separate each number.
By default, a space is used. Use the sequence \n to specify
a newline.
X -N -- Shortcut for using a newline separator.
X -e
-- Exclude the numbers in from the output. This is so that if
you want to do a complex range without including certain numbers.
is a list of numbers seperated by a ','.
-- random --
Print out a random number or numbers. This program will take a range of
numbers much like the range command does. Except that instead of printing out
all the numbers in that range, it will print out a random number from that
range.
Ex:
$ random 1..100
37
$ random 1.0..100.0
56.9
$ random 1.000..100.000
42.397
$ random 2..100i2 [only pick among the even integers from 2 to 100]
68
Usage options (in addition to the standard options)
-n -- Generate random numbers seperated by a space.
-s -- Use as the seperation character.
-N -- Shortcut for using a newline separation character.
Feature submitted by areiner@tph.tuwien.ac.at:
- You should specify the distribution you are generating;
probably, this is just an equal distribution. What would come in handy
quite often is to have a way of producing pseudo-random numbers that
realize a chosen distribution with a given set of parameters. But that
should probably be a different program than random, e.g. distribution,
as the name is already taken. So, e.g., ``distribution --gaussian 3.0
4.0 100'' should produce 100 numbers distributed according to a gauss
distribution with mean 3 and variance 4.
-- bound --
This program is for finding the maximum, minimum and surrounding numbers in a
set. You can use different options to specify how many numbers are returned as
to show top maximum and minimum lists. Also you can show the numbers that
occur around the context of the boundary numbers.
Ex:
$ cat numbers
1 10 8 100 15 1000
2 9 5 15 27 12 136
$ bound -u numbers (upper)
1000
$ bound -l numbers (lower)
1
Top 4 upper numbers sorted
$ bound -u -n 4 numbers
1000
136
100
27
Bottom 4 lower numbers sorted)
$ bound -l -n 4 numbers
1
2
5
8
Context around lowest number. Show 2 numbers of context.
$ bound -l -c 2 numbers
1 10 8
Find the 5 closet numbers to the number 75. They will print out
in closeness order.
$ bound -f 75 -n 6
100
27
15
15
136
10
Usage options (in addition to the standard options)
-u -- Return the upper bound number in the set (the maximum number)
-l -- Return the lower bound number in the set (the minimum number)
-n -- Return the top or bottom numbers or numbers around
a number.
-c -- In addition to the number returned, show numbers on both sides
of the number.
-f -- Find the number or the closest number to .
-- numprocess -- (maybe a name like mutate, alter, or process would be better)
This program mutates numbers as it encounters them. It should do the
following operations:
o Add/Subtract a value to a number
o Multiply/Divide a number by a factor
o Raise a number to a power (includes the concept of roots)
o Round a number up or down
o Do any mathematical function to a number.
o Quantify a number. So 1024 bytes is 1KB and so on.
Ex:
Add 1 to each number
$ numprocess /+1/
Multiply each number by 8 and then divide by 5
$ numprocess /*8,%5/
Convert from Farenheit to Celcius
$ numprocess /-32,*5,%9/
Usage options (in addition to the standard options)
-- interval --
This program calculates and displays the interval between one number
and the next.
num-utils-0.5/LICENSE0100664000076400007640000000165407665476706013134 0ustar susosuso---------------------------------------------------------------------------
num-utils - A set of programs that deals with numbers.
Copyright (C) 2002-2003 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.
num-utils-0.5/MANIFEST0100664000076400007640000000027007734144204013227 0ustar susosusoCHANGELOG
COPYING
GOALS
LICENSE
MANIFEST
Makefile
README
VERSION
WARNING
num-utils.spec.in
template
numsum
numgrep
average
round
range
random
bound
numprocess
tests
interval
normalize
num-utils-0.5/Makefile0100664000076400007640000000762307734144204013547 0ustar susosuso# $Id: Makefile,v 1.12 2003/09/23 22:26:12 suso Exp $
# num-utils A set of programs for doing operations on numbers
# Copyright (C) 2002-2003 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 .
VERSION = $(shell cat VERSION)
PROJECT = num-utils
DIST = $(PROJECT)-$(VERSION)
FILES = $(shell cat MANIFEST)
UTILS = average bound interval normalize numgrep numprocess numsum random range round
DOCS = CHANGELOG COPYING LICENSE MANIFEST template README GOALS WARNING
TESTS = file fractionalnums numbers numbers2 README zeros
# rpm --showrc is gettin to be hard to parse anymore.
#RPMDIR = /usr/src/redhat
RPMDIR = $(shell rpm --showrc | grep " _topdir" | \
perl -n -e \
'/_topdir(?:\s+|\s+:\s+)(\/.*|%{_usrsrc}.*$$)/; \
$$dir = $$1; $$dir =~ s|%{_usrsrc}|/usr/src|; print "$$1\n";')
# Modify these as nessecary
PERL = /usr/bin/perl
BINDIR = /usr/bin
TOPDIR = $(ROOT)/usr
MANDIR = $(TOPDIR)/share/man/man1
DOCDIR = $(TOPDIR)/share/doc/$(DIST)
BINDIR = $(TOPDIR)/bin
TARFILE = $(DIST).tar.gz
RPMFILE = $(RPMDIR)
SPECFILE= $(PROJECT).spec
RPMFILE = $(RPMDIR)/RPMS/noarch/$(DIST)-1.noarch.rpm
SRPMFILE= $(RPMDIR)/SRPMS/$(DIST)-1.src.rpm
all: manpages
for file in $(UTILS) ; do \
cat $$file | sed 's|^#!/usr/bin/perl|#!$(PERL)|' > $$file.out ; \
mv $$file.out $$file ; chmod a+x $$file ; done
install:
install -m 0755 -o 0 -g 0 -d $(BINDIR)
for util in $(UTILS) ; do \
install -m 0755 -o 0 -g 0 $$util $(BINDIR) ; done
install -m 0755 -o 0 -g 0 -d $(DOCDIR)
for doc in $(DOCS) ; do \
install -m 0644 -o 0 -g 0 $$doc $(DOCDIR) ; done
install -m 0755 -o 0 -g 0 -d $(MANDIR)
for man in $(UTILS) ; do \
install -m 0644 -o 0 -g 0 $$man.1.gz $(MANDIR) ; done
uninstall:
for util in $(UTILS) ; do \
rm -f $(BINDIR)/$$util ; done
rmdir $(BINDIR)
for doc in $(DOCS) ; do \
rm -f $(DOCDIR)/$$doc ; done
rmdir $(DOCDIR)
for man in $(UTILS) ; do \
rm -f $(MANDIR)/$$man.1.gz ; done
rmdir $(MANDIR)
manpages:
for doc in $(UTILS) ; do \
pod2man $$doc > $$doc.1 ; \
gzip $$doc.1 ; chmod 644 $$doc.1.gz \
; done
rpminstall:
install -m 0755 -d $(BINDIR)
for util in $(UTILS) ; do \
install -m 0755 $$util $(BINDIR) ; done
install -m 0755 -d $(DOCDIR)
for doc in $(DOCS) ; do \
install -m 0644 $$doc $(DOCDIR) ; done
install -m 0755 -d $(MANDIR)
for man in $(UTILS) ; do \
install -m 0644 $$man.1.gz $(MANDIR) ; done
clean:
rm -f $(PROJECT).spec
rm -f $(TARFILE)
for file in $(UTILS) ; do \
rm -f $$file.1 ; done
for file in $(UTILS) ; do \
cat $$file | sed 's|^#!$(PERL)|#!/usr/bin/perl|' > $$file.out ; \
mv $$file.out $$file ; chmod a+x $$file ; done
dist: $(TARFILE)
test:
for file in $(UTILS) ; do $(PERL) -Tcw $$file ; done
$(TARFILE): $(FILES)
rm -fr $(DIST)
rm -f $(TARFILE)
mkdir $(DIST)
perl -pe 'chomp; symlink "../$$_", "$(DIST)/$$_" or die $$!' MANIFEST
tar zchf $(DIST).tar.gz $(DIST) --exclude CVS
rm -r $(DIST)
rpm: $(RPMFILE) $(SRPMFILE)
$(RPMFILE): do-rpm
$(SRPMFILE): do-rpm
do-rpm: $(TARFILE) $(SPECFILE)
cp $(SPECFILE) $(RPMDIR)/SPECS/
cp $(TARFILE) $(RPMDIR)/SOURCES/
rpmbuild -ba $(RPMDIR)/SPECS/$(SPECFILE)
$(SPECFILE): $(SPECFILE).in VERSION
perl -pe 's[\@(\S+)\@] \
[open F, $$1 or die; $$x = join "", ; chomp $$x; $$x]ge' \
< $< > $@
num-utils-0.5/README0100664000076400007640000000573107667227206012776 0ustar susosuso
num-utils is Copyright 2002-2003 Suso Banderas
====================----------------------------
**PLEASE ALSO READ THE WARNING FILE THAT**
**COMES WITH THIS PROGRAM.**
Description - num-utils are a set of utilities that aim to provide
the numeric equivilent of the GNU text-utils. These programs do
operations on numbers such as filtering, adding, averaging and so on.
They help expand and complete the shell vocabulary.
Right now these programs are in the beta stage. If you have any bug
fixes or suggestions to improve this program, please see the "Contact"
section of this document . An extensive list of planned features is
in the 'GOALS' file included with this program.
Installation
------------
For now please use the Makefile to install the programs. By default
this will install them in /usr/bin. Standard 'make' and 'make install'
commands will build and install the programs. You can also make an RPM
source and binary package from the Makefile using 'make rpm'.
How to use
----------
These programs are useful when you want to deal with numbers on stdin or
from files. There are different programs for different functions:
numsum - Adding a set of numbers from stdin is simply a matter of passing
just the numbers themselves to numsum.
$ cat test/numbers | numsum
21937.74
$
You can use other programs like numgrep, awk or sed to get just the numbers
that you want from a file:
$ cat access_log | awk {'print $10'} | numsum
numgrep - Simular to grep, but specific to numbers. This program will let
you search for ranges of numbers and sequences.
$ numgrep /-5..5,100..200/ tests/numbers
5
135
135
1
1.5
4.5351
3
3
-1
-2.543
-1
3
random - Generates random numbers based on range expressions.
$ randnum /0..100/
47
$
range - This program will spit out a sequence of numbers for use in stuff
like shell for loops. The syntax for this program will most likely
change in the next version.
$ range /5..10/
5 6 7 8 9 10
$
$ for i in `range /1..5,10..15/` ; do mv track$i.mp3 song$i.mp3 ; done
1
2
3
4
5
10
11
12
13
14
15
$
interval - This program will calculate and display the interval between
every two seqeuntial numbers on the input stream:
$ cat numbers
1
2
3
5
10
45
35
2
1
$ cat numbers | interval
1
1
2
5
35
-10
-33
-1
$
Contact
-------
Please send bug reports, suggestions, requests or correspondence to Suso Banderas
at 'suso@suso.org'. You can visit the homepage for this program at:
http://suso.suso.org/programs/num-utils/
num-utils-0.5/VERSION0100664000076400007640000000000410147276245013144 0ustar susosuso0.5
num-utils-0.5/WARNING0100664000076400007640000000133207502661631013127 0ustar susosusonum-utils - WARNING
-----------------------------------
The num-utils are not nearly tested as much as they should be and does
not have all the functionality that you may expect it to have. I'm
releasing them now so that I can get a little bit of input from
potential users of the programs and also so that I could reach a checkpoint
in their development. If you have a problem with num-utils please notify
me at 'suso@suso.org' and I will take note of the bug or your comments.
It should also be noted that options and expected behavior are likely
to change before the release of version 1.0. Right now I'm trying to
find the best interface for these programs.
Thanks for being patient,
Suso Banderas
suso@suso.org
num-utils-0.5/num-utils.spec.in0100664000076400007640000000400010147276245015312 0ustar susosuso# $Id: num-utils.spec.in,v 1.12 2004/11/19 04:55:01 suso Exp $
Summary: num-utils are a set of programs for dealing with numbers.
Name: num-utils
Version: @VERSION@
Release: 1
Copyright: GPL
Group: Utilities
Source: num-utils-@VERSION@.tar.gz
BuildArch: noarch
BuildRoot: /var/tmp/num-utils-build-@VERSION@
Packager: Suso Banderas
Vendor: suso.org
%description
The num-utils, short for numeric utilities are a set of programs designed
to work together from the unix shell to do numeric operations on input.
They are basically the numeric equivilent of common unix text utilities
and aim to help complete the unix shell vocabulary.
%prep
%setup
%build
make
%install
make ROOT="$RPM_BUILD_ROOT" rpminstall
%files
%attr(0755 root root) /usr/bin/average
%attr(0755 root root) /usr/bin/bound
%attr(0755 root root) /usr/bin/interval
%attr(0755 root root) /usr/bin/normalize
%attr(0755 root root) /usr/bin/numgrep
%attr(0755 root root) /usr/bin/numprocess
%attr(0755 root root) /usr/bin/numsum
%attr(0755 root root) /usr/bin/random
%attr(0755 root root) /usr/bin/range
%attr(0755 root root) /usr/bin/round
%doc %attr(- root root) CHANGELOG COPYING GOALS LICENSE README WARNING template
#%attr(0755 root root) tests
%doc %attr(- root root) tests/
%{_mandir}/man1/average.1.gz
%{_mandir}/man1/bound.1.gz
%{_mandir}/man1/interval.1.gz
%{_mandir}/man1/normalize.1.gz
%{_mandir}/man1/numgrep.1.gz
%{_mandir}/man1/numprocess.1.gz
%{_mandir}/man1/numsum.1.gz
%{_mandir}/man1/random.1.gz
%{_mandir}/man1/range.1.gz
%{_mandir}/man1/round.1.gz
%changelog
* Fri Nov 19 2004 Suso Banderas
- 0.5 release
* Tue Sep 23 2003 Suso Banderas
- 0.4 release
- added file entries for normalize program.
* Mon Jun 9 2003 Suso Banderas
- 0.3 beta release
* Tue Jun 3 2003 Suso Banderas
- Added file entries for interval program.
* Thu May 29 2003 Suso Banderas
- 0.2 beta release.
* Mon Jun 17 2002 Suso Banderas
- Initial 0.1 release of the num-utils
num-utils-0.5/template0100775000076400007640000000715410147276246013653 0ustar susosuso#!/usr/bin/perl -w
# template: This is the default template file used to create
# new files.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
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.
}
################
# 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, \@number_array) ||
$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, \@number_array);
}
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
----------------------------------------------
template :
----------------------------------------------
Usage:
template [options]
| template [options]
template [options]
Options:
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $number_array_ref = shift;
while (<$filehandle>) {
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
print STDERR "found number: $1\n" if ($verbose >= 3);
push(@$number_array_ref, $1);
}
}
return 1;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
template - template program file
=head1 SYNOPSIS
B [-dhV]
| B [-dhV] (Input on STDIN from pipeline.)
B [-dhV] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
will .......
=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), numprocess(1), numsum(1), random(1), range(1), round(1)
=head1 COPYRIGHT
template 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
submitions or help for the project.
=head1 MORE INFO
More info on template can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/numsum0100775000076400007640000002342510147276245013362 0ustar susosuso#!/usr/bin/perl -w
# numsum: This program adds up all numbers it encounters and prints out
# the total at the end on STDOUT.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
#use strict;
use vars qw/ %opts /;
my ($file, $finalsum, @number_array, @columns_to_print, @column_output, $output_line, $string_seperator, $verbose);
getopts('123456789cdhiIqrs:Vx:y:', \%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; # No output except the final answer.
} else {
$verbose = 1; # Normal output.
}
# I'm not sure whether this should stay in or not. It's mainly just for convience.
# It's a bad hack because you can only specify 1 through 9. Maybe sometime I'll figure
# out how to bypass getopts to specify extra options.
for ($n = 1 ; $n < 10; $n++) {
if ($opts{$n}) {
$opts{'c'} = 1;
$opts{'x'} = $n;
}
}
if ($opts{'x'}) {
@columns_to_print = split(/,/, $opts{'x'});
}
if ($opts{'y'}) {
@rows_to_print = split(/,/, $opts{'y'});
}
if ($opts{'s'}) {
$string_seperator = $opts{'s'};
} else {
$string_seperator = "\\s+";
}
################
# MAIN PROGRAM #
################
if (@ARGV) {
foreach $file (@ARGV) {
print STDERR "Reading from file $file.\n" if ($verbose >= 2);
open (ARGFILE, "$file") && process_filehandle(\*ARGFILE, \@number_array)
|| $verbose && warn "Couldn't open file $file for reading: $!\n";
close(ARGFILE);
}
} else {
print STDERR "Reading from STDIN.\n" if ($verbose >= 2);
process_filehandle(\*STDIN, \@number_array);
}
my $number_array_length = @number_array;
if ($opts{'c'}) { # Handle column sum output.
my @column_output = ();
my $i;
for ($i = 0; $i < $number_array_length; $i++) {
my @add_this_array = split(/$string_seperator/, $number_array[$i]);
sum_columns(\@column_output, \@add_this_array);
}
# Now print out the columular output.
if ($opts{'x'}) {
my @final_output = ();
foreach $column_number (@columns_to_print) {
push (@final_output, $column_output[$column_number-1]);
}
$output_line = join(" ", @final_output);
} else {
$output_line = join(" ", @column_output);
}
print "$output_line\n";
} elsif ($opts{'r'}) { # Handle row sum output.
my $i;
if ($opts{'y'}) {
foreach $row_number (@rows_to_print) {
my @add_this_array = split(/ /, $number_array[$row_number-1]);
my $row_sum = 0;
foreach $row_value (@add_this_array) {
$row_sum += $row_value;
}
print "$row_sum\n";
}
} else {
for ($i = 0; $i < $number_array_length; $i++) {
my @add_this_array = split(/ /, $number_array[$i]);
my $row_sum = 0;
foreach $row_value (@add_this_array) {
next if ($row_value !~ m/^[0-9]+$/);
$row_sum += $row_value;
}
print "$row_sum\n";
}
}
} else {
$finalsum = add_array(\@number_array);
if ($opts{'i'}) {
$finalsum = int($finalsum);
} elsif ($opts{'I'}) {
if ($finalsum == int($finalsum)) {
$finalsum = 0;
} else {
$finalsum =~ s/^(\-?)[0-9]*\.([0-9]*)$/$1.$2/;
}
}
print "$finalsum\n";
}
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
---------------------------------------------------------------------------
numsum : A program that adds up all numbers of input and returns the sum.
---------------------------------------------------------------------------
Usage:
numsum [options] (Input from a file.)
| numsum [options] (Input from command pipeline.)
numsum [options] (Input on STDIN. Use Ctrl-D to stop.)
Options:
-i Only return the integer portion of the final sum.
-I Only return the decimal portion of the final sum
-c Print out the sum of each column.
-r Print out the sum of each row.
-x Specify a comma seperated list of columns to print.
-y Specify a comma seperated list of rows to print.
-s Specify a seperator string for spliting columns.
This defaults to consecutive whitespace.
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
-q Quiet mode, don't print any warnings.
EOF
}
# This function is used so that we can genericize what filehandle
# we are getting our information from.
sub process_filehandle {
my $filehandle = shift;
my $number_array_ref = shift;
while (<$filehandle>) {
my $line = $_;
chomp($line);
if ($opts{'c'} || $opts{'r'}) { # Process columns or rows
# Make each line column friendly by changing all non-numeric words into 0.
# Some ideas from the #perl channel on irc.freenode.org. Thanks to v, dkr, Khisanth and dudeman
#$input = join ' ', map { /\D/ ? 0 : $_ } split / +/, $input;
#s!(\S+)!$1=~/\D/?0:$1!ge;
# This one works best.
$line =~ s!(\S+)!$1=~/\D/?0:$1!ge;
push(@$number_array_ref, $line);
} else { # Normal processing.
if ($line =~ /^\s*(\-?[0-9]*\.?[0-9]+)/) {
print STDERR "number: $1\n" if ($verbose >= 3);
push(@$number_array_ref, $1);
}
}
}
return 1;
}
# Function for adding up numbers
sub add_array {
my $arrayref = shift;
my $runningtotal = 0;
my $number;
foreach $number (@$arrayref) {
print STDERR "$number\n" if ($verbose >= 2);
$runningtotal += $number;
}
return $runningtotal;
}
# Function for summing up the columns.
sub sum_columns {
my $sum_array_ref = shift;
my $input_array_ref = shift;
my $input_array_length = @$input_array_ref;
my $x;
for ($x = 0; $x < $input_array_length; $x++) {
if (${$sum_array_ref}[$x]) {
${$sum_array_ref}[$x] += ${$input_array_ref}[$x];
} else {
${$sum_array_ref}[$x] = ${$input_array_ref}[$x];
}
}
return 1;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
numsum - numsum program file
=head1 SYNOPSIS
B [-iIcdhrsvxy]
| B [-iIcdhrsvxy] (Input on STDIN from pipeline.)
B [-iIcdhrsvxy] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
will take all the numbers on stdin and return the sum of those numbers. Currently
it only processes the first number on each line. Besides positive numbers, it also
handles negative numbers and numbers with decimals.
=head1 OPTIONS
-i Only return the integer portion of the final sum.
-I Only return the decimal portion of the final sum.
-c Print out the sum of each column.
-r Print out the sum of each row.
-x Specify a comma seperated list of columns to print.
-y Specify a comma seperated list of rows to print.
-s Specify a string to use as a seperator for columns.
This defaults to be consecutive whitespace (\s+).
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
-q Quiet mode, don't print any warnings.
=head1 EXAMPLES
Simply add up the numbers in a file.
$ numsum numbers.txt
4315
Enter your own numbers on STDIN. The last number is the answer.
$ numsum
4
21
98
100
223
Use it in a command pipeline.
$ ls -1s | grep .mp3 | numsum -c -x 5
72288
Add up the total byte count in a http log file.
$ cat access_log | awk {'print $10'} numsum
or
numsum -c -x 10 access_log
Add up the columns of numbers of a file.
$ cat columns
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
$ numsum -c columns
15 40 65 90 115
Add up the 1st, 2nd and 5th columns only.
$ numsum -c -x 1,2,5 columns
15 40 115
Add up the rows of numbers of a file.
$ numsum -r columns
55
60
65
70
75
Add up the 2nd and 4th rows.
$ numsum -r -y 2,4 columns
60
70
=head1 SEE ALSO
average(1), bound(1), interval(1), normalize(1), numgrep(1), numprocess(1), random(1), range(1), round(1)
=head1 COPYRIGHT
numsum 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
submitions or help for the project.
=head1 MORE INFO
More info on numsum can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/numgrep0100775000076400007640000002534610147276245013517 0ustar susosuso#!/usr/bin/perl -w
# numgrep: This program is the numeric equivilent of the grep
# utility. It searches for numbers, sets of numbers and so on.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
$| = 1;
getopts('hdVl', \%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; # No output except the final answer.
} else {
$verbose = 1; # Normal output.
}
my $match_expression = shift;
$match_expression =~ s/^\/(.*)\/$/$1/;
# Now read in any extra arguments as filenames to do searches in.
my @files = @ARGV;
print STDERR "Arg files: " . @files . "\n" if ($verbose >= 3);
my @ranges;
my $file;
################
# MAIN PROGRAM #
################
if (@ARGV) {
foreach $file (@ARGV) {
print STDERR "Reading from file $file.\n" if ($verbose >= 2);
open (ARGFILE, "$file") && process_filehandle(\*ARGFILE, $match_expression)
|| $verbose && warn "Couldn't open file $file for reading: $!\n";
close(ARGFILE);
}
} else {
print STDERR "Reading from STDIN.\n" if ($verbose >= 2);
process_filehandle(\*STDIN, $match_expression);
}
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
-----------------------------------------------------------
numgrep: Search for a number using numeric expressions.
-----------------------------------------------------------
Usage:
numgrep [options] /expression/ file
| numgrep [options] /expression/
numgrep [options] /expression/
Options:
-l Print the matching numbers out one per line
instead of printing the entire line they are on.
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
Expressions:
.. Range. Use to specify a range of numbers such
as 1..10, 1.. or ..10. Which mean between 1 and 10, greater than
1, and less than 10 respectively
, Expression seperator. The comma seperates expressions within
the forward slashes. ie. /1..10,20..30,50,60,127/
m Multiple operator. Using the m followed by an integer will match
any multiple of that integer. So /m3/ would match 3, 6, 9, 12, etc.
f Factor operator. Using the f followed by an integer will match
any factor of that integer. So /m20/ would match 1, 2, 4, 5 and 10.
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $returnvalue;
# The below array holds individual expressions that
# will be looped through to find matching values.
while (<$filehandle>) {
my $line = $_;
if ($line =~ /[0-9]/) { # This expression will determine whether we
# should even try to match. Maybe it's wrong
# to do this, but it will speed things up.
num_match($line);
} else {
next;
}
}
return 1;
}
# This subroutine determines what to do with matched lines.
sub num_match {
my $line = shift;
my $returnvalue = 0;
my $expression;
my $number;
my @expression_array = split(/,/, $match_expression);
my @numbers = ();
my $strlen = length($line);
my $n = 0;
while ($n < $strlen) {
my $char = substr($line, $n, 1);
if ($char =~ /[0-9\.-]/) {
($n, $number) = parse_number(substr($line, $n, ($strlen - $n)), $n);
unless ($number eq "") {
push (@numbers, $number);
}
} else {
$n++;
}
}
if ($opts{'l'}) { # Follow this branch if the user just wants the numbers and not the context.
foreach $number (@numbers) { # SET represents the numbers found on one line.
chomp($number);
EXP: foreach $expression (@expression_array) {
print "Expression: $expression\n" if ($verbose >= 3);
my $exp = make_expression($expression, $number);
# Now if the $exp is true then go on.
if (eval($exp)) {
print "$number\n";
last EXP;
}
}
}
} else { # Else we'll just print out lines that match one of the expressions.
NUM: foreach $number (@numbers) {
foreach $expression (@expression_array) {
my $exp = make_expression($expression, $number);
if (eval($exp)) {
print "$line";
last NUM;
}
}
}
}
return $returnvalue;
}
sub make_expression {
my $expression = shift;
my $number = shift;
my $exp;
if ($expression =~ /^(-?[0-9\.]+)\.\.(-?[0-9\.]+)$/) { # normal range between two numbers.
$exp = "$1 <= $number && $number <= $2";
} elsif ($expression =~ /^\.\.(-?[0-9\.]+)$/) { # Anything lower than X
$exp = "$number <= $1";
} elsif ($expression =~ /^(-?[0-9\.]+)\.\.$/) { # Anything higher than X
$exp = "$number >= $1";
} elsif ($expression =~ /^(-?[0-9\.]+)$/) {
$exp = "$number == $1";
} elsif ($expression =~ /^m(-?[0-9\.]+)$/) { # Multiples.
if ($1 =~ /\./) {
die "Please use only integer values for m.\n";
$exp = "$number =~ /$expression/";
} else {
$exp = "!($number % $1)";
}
} elsif ($expression =~ /^f(-?[0-9\.]+)$/) { # Factors.
if ($1 =~ /\./) {
die "Please use only integer values for f.\n";
$exp = "$number =~ /$expression/";
} else {
$exp = "!($1 % $number)";
}
} else {
$exp = "$number =~ /$expression/";
}
print "Exp: '$exp'\n" if ($verbose >= 3);
return $exp
}
sub parse_number {
my $substring = shift;
my $placeholder = shift;
$substring =~ /^(-?[0-9]*\.?[0-9]*)[^0-9]/;
my $number = $1;
my $strlen;
if ($number =~ /^[\.-]*$/) {
$strlen = length($number);
$number = "";
} else {
$strlen = length($number);
}
# Fix the number somewhat.
#$number =~ s/^\./0./;
#$number =~ s/^-\./-0./;
#$number =~ s/-0+/0/;
#$number =~ s/\.$//;
chomp($number);
return ($placeholder + $strlen, $number);
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
numgrep - This program is the numeric equivilent of the grep utility.
=head1 SYNOPSIS
B [-dhlV]
| B [-dhlV] (Input on STDIN from pipeline.)
B [-dhlV] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B searches for different occurances of numbers through the use
of numeric expressions.
=head1 OPTIONS
-l Print the matching numbers out one per line
instead of printing the entire line they are on.
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
=head1 EXPRESSIONS
B uses a special numeric expression matching system. Basically,
it searches for ranges, factors and sequences of numbers. Here is a list
of the syntax characters and some sample expressions that will get you
going:
//
Put your expression or set of expressions between these
two forward slashes.
.. Range expression. A number must be used on the left
and/or right of this expression to specify that numbers
between, greater than or less than the numbers specified
should be matched.
, Expression sepeartor. The comma sepearates one complete
expression from another in a set enclosed by //.
m Multiples of . This operator, followed by a number
will match any number that is an integer
multiple of . Meaning that = times ,
where is any integer.
f Factors of . This operator, followed by a number
will match any number that is an integer factor of
. Meaning that = divided by , where
is any integer.
B< NOTE:> Checking for factors and multiples is very fast because it
is checked by doing a single modulus operation on two numbers.
B
/2..10/ Match any number between 2 and 10.
/2..10,20..30/ Match any number between 2 and 10 or between 20 and 30.
/56,34,512,45,67/ Match any of the numbers 56, 34, 512, 45 or 67.
/m3/ Match any integer that is a multiple of 3.
/f1024/ Match any integer that is a factor of 1024.
$ range -N /1..1000/ | numgrep /f1024/
1
2
4
8
16
32
64
128
256
512
$
=head1 BUGS
B can't handle certain situations properly. Such as if
it encounters a number with leading zeros, it will treat it as an
octal number and thus might not match the way you would expect.
B does not yet allow you to mix numbers and text in the
matching expression. So you can not do something like
'numgrep /port=0..1023/ firewall.log'. But this will be changed in the
future.
=head1 SEE ALSO
average(1), bound(1), interval(1), normalize(1), numprocess(1), numsum(1), random(1), range(1), round(1)
=head1 COPYRIGHT
numgrep 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
submitions or help for the project.
=head1 BUGS
numgrep will round decimal numbers with more than 15 digits of accuracy. This is
mostly due to limit's in the way programming languages deal directly with numbers.
=head1 MORE INFO
More info on numgrep can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/average0100775000076400007640000001632410147276245013450 0ustar susosuso#!/usr/bin/perl -w
# average: Find the average of a set of numbers.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
getopts('dhiIlmMqV', \%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 ($file, @number_array, $number, $total_numbers, @numbers, $strlen);
my $sum_total = 0;
################
# 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, \@number_array) ||
$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, \@number_array);
}
my $average;
if ($opts{'m'}) {
$average = find_mode(\@number_array);
} elsif ($opts{'M'}) {
$average = find_median(\@number_array);
} else {
$average = calculate_mean(\@number_array);
}
# post processing.
if ($opts{'i'}) {
$average = int($average);
} elsif ($opts{'I'}) {
if ($average == int($average)) {
$average = 0;
} else {
$average =~ s/^(\-?)[0-9]*\.([0-9]*)$/$1.$2/;
}
}
print "$average\n";
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
---------------------------------------------------------
average : A program for finding the average of numbers.
---------------------------------------------------------
Usage:
average [options] file
| average [options]
average [options]
Options:
-i Only return the integer portion of the final sum.
-I Only return the decimal portion of the final sum
-m Find the mode (most occuring) of the list of numbers.
-M Find the median (middle number) of the list of numbers.
-l When finding the median and the count of numbers in the set is even,
use the lower middle number instead of the upper middle number.
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
-q Quiet mode, don't print any warnings.
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $number_array_ref = shift;
while (<$filehandle>) {
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
print STDERR "found number: $1\n" if ($verbose >= 3);
push(@$number_array_ref, $1);
}
}
return 1;
}
sub calculate_mean {
my $number_array_ref = shift;
my $number_of_numbers = @$number_array_ref;
my $runningtotal = 0;
foreach $number (@$number_array_ref) {
$runningtotal += $number;
print STDERR "adding number: $number (total: $runningtotal)\n" if ($verbose >= 3);
}
$average = $runningtotal / $number_of_numbers;
print STDERR "averaging numbers: $runningtotal total / $number_of_numbers numbers = $average\n" if ($verbose >= 2);
return $average;
}
sub find_mode {
my $number_array_ref = shift;
my (%numbers_count,$highest_number,$highest_count);
foreach $number (@$number_array_ref) {
$numbers_count{$number}++;
if (!defined($highest_number)) {
$highest_number = $number;
$highest_count = 1;
} elsif ($numbers_count{$number} > $highest_count) {
$highest_number = $number;
$highest_count++;
print STDERR "Highest number is now: $highest_number with a count of $highest_count.\n" if ($verbose >= 2);
}
}
return $highest_number;
}
# This routine will find the median of a set of numbers.
# If the set of numbers counts to be even, then it will take
# the higher indexed number in the middle of the set.
# If the -l option is set, it will take the lower index
# number in the middle.
sub find_median {
my $number_array_ref = shift;
my @number_array = sort {$a <=> $b} @$number_array_ref;
my $array_count = scalar(@number_array);
my $median_index;
print "The array count is: $array_count\n" if ($verbose >= 2);
if (($array_count % 2) == 0) { # The index count is even.
if ($opts{'l'}) {
$median_index = int($array_count / 2);
} else {
$median_index = int(($array_count / 2) + 1);
}
} else {
$median_index = int(($array_count / 2) + 0.5);
}
$median_index--; # Translate the number to 0 based array set.
print "The median index is: " . int(($array_count / 2) - 0.5) . "\n" if ($verbose >= 2);
my $median = $number_array[$median_index];
return $median;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
average - Find the average of a set of numbers.
=head1 SYNOPSIS
B [-dhiIlmMV]
| B [-dhiIlmMV] (Input on STDIN from pipeline.)
B [-dhiIlmMV] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. By default
B will determine the average from all numbers on input. Other
options allow you to find the mode and median.
=head1 OPTIONS
-i Only return the integer portion of the final sum.
-I Only return the decimal portion of the final sum
-m Find the mode (most occuring) of the list of numbers.
-M Find the median (middle number) of the list of numbers.
-l When finding the median and the count of numbers in the set is even,
use the lower middle number instead of the upper middle number.
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
=head1 SEE ALSO
bound(1), interval(1), normalize(1), numgrep(1), numprocess(1), numsum(1), random(1), range(1), round(1)
=head1 COPYRIGHT
average 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
submitions or help for the project.
=head1 MORE INFO
More info on average can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/round0100775000076400007640000001400710147276246013162 0ustar susosuso#!/usr/bin/perl -w
# round: A program that rounds off numbers it encounters.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
use POSIX; # for rounding functions.
getopts('cfn:hdV', \%opts);
my ($file, @number_array, $verbose);
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.
}
# If the -n option is set, use the factor from that, otherwise use a factor
# of 1 for standard rounding.
my $factor = $opts{'n'} || 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, \@number_array) ||
$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, \@number_array);
}
#process_numbers(\@number_array);
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
---------------------------------------------------------
round : A program that rounds off numbers it encounters.
---------------------------------------------------------
Usage:
round [options]
| round [options]
round [options]
Options:
-c Force the number to be rounded up. Ceiling.
-f Force the number to be rounded down. Floor.
-n Round numbers to the nearest factor of .
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $number_array_ref = shift;
while (<$filehandle>) {
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
my $number = $1;
print STDERR "found number: $1\n" if ($verbose >= 3);
my $newnumber;
print STDERR "original number: $number\n" if ($verbose >= 2);
if ($opts{'c'}) {
$newnumber = ceiling_number($number);
} elsif ($opts{'f'}) {
$newnumber = floor_number($number);
} else {
$newnumber = round_number($factor, $number);
}
print "$newnumber\n";
}
}
return 1;
}
# The simple rounding function. In the next version, this function
# will be able to round to arbitrary factors of any number.
# The algorithm for this subroutine was inspired by the nearest subroutine
# in Geoffrey Rommel's Math::Round perl module. Thanks Geoffrey.
sub round_number {
my $factor = shift;
my $number = shift;
my $rounded_number;
$factor = abs($factor);
if ($number >= 0) {
$rounded_number = $factor * floor(($number + 0.5 * $factor) / $factor);
} else {
$rounded_number = $factor * ceil(($number - 0.5 * $factor) / $factor);
}
return $rounded_number;
}
sub ceiling_number {
my $number = shift;
my $ceiling_number = ceil($number);
return $ceiling_number;
}
sub floor_number {
my $number = shift;
my $floor_number = floor($number);
return $floor_number;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
round - A program that rounds off numbers it encounters.
=head1 SYNOPSIS
B [-cdfhV]
| B [-cdfhV] (Input on STDIN from pipeline.)
B [-cdfhV] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
will simply round decimal numbers to the nearest integer or to a factor of any
number using the -n option. You can also force it to round up (ceiling) or
round down(floor) using the -c and -f options.
=head1 OPTIONS
-c Force the number to be rounded up. Ceiling.
-f Force the number to be rounded down. Floor.
-n Round numbers to the nearest factor of .
-h Print out some helpful information about round.
-V Increase verbosity. This will print out numbers as it finds them.
-d Debug mode. For developers only.
=head1 BUGS
B currently only rounds the first number on each line. Eventually this
will be changed as all the numeric utilities are developed.
B will drop off the decimal places in decimal numbers. This may cause
some calculations to be in error, depending on how you are using the data.
=head1 SEE ALSO
average(1), bound(1), interval(1), normalize(1), numgrep(1), numprocess(1), numsum(1), random(1), range(1)
=head1 COPYRIGHT
round 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
submitions or help for the project.
=head1 MORE INFO
More info on round can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/range0100775000076400007640000002012710147276246013127 0ustar susosuso#!/usr/bin/perl -w
# range: Print out a range of numbers for use in for loops and such.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
my ($expression, @output_array, $number);
getopts('e:n:Np:s:hdV', \%opts);
my $prefix = "";
my $suffix = "";
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 $range_expression = shift || die "You must specify a range expression\n";
# A shortcut for specifying a prefix and suffix.
$range_expression =~ m/^([^\/]+)\//;
$prefix = $1;
$range_expression =~ s/^[^\/]+\//\//;
$range_expression =~ m/\/([^\/]+)$/;
$suffix = $1;
$range_expression =~ s/\/[^\/]+$/\//;
$range_expression =~ s/^\///;
$range_expression =~ s/\/$//;
my @range_expressions = split(/,/, $range_expression);
my $separator = $opts{'n'} || " ";
$separator =~ s/\\n/\n/g;
$separator = "\n" if ($opts{'N'});
if ($opts{'p'}) {
$prefix = $opts{'p'};
}
if ($opts{'s'}) {
$suffix = $opts{'s'};
}
# Make the array of excluded numbers
my @excludes;
if ($opts{'e'}) {
@excludes = split(/,/, $opts{'e'});
}
################
# MAIN PROGRAM #
################
my $number_exp = "-?[0-9]*\\.?[0-9]+";
foreach $expression (@range_expressions) {
print "expression: $expression\n" if ($verbose >= 3);
if ($expression =~ /^($number_exp)$/) {
push @output_array, $expression;
} elsif ($expression =~ /^($number_exp)\.\.($number_exp)$/) {
print "1: $1\n2: $2\n" if ($verbose >= 3);
if ($1 > $2) {
my $number = $1;
while ($number >= $2) {
next if grep(/^$number$/, @excludes);
push (@output_array, $number);
$number--;
}
} else {
my $number;
foreach $number ($1..$2) {
next if grep(/^$number$/, @excludes);
push (@output_array, $number);
}
}
} elsif ($expression =~ /^($number_exp)\.\.($number_exp)i($number_exp)$/) {
print "1: $1\n2: $2\n3: $3\n" if ($verbose >= 3);
if ($1 > $2) {
my $number = $1;
while ($number >= $2) {
next if grep(/^$number$/, @excludes);
$number = eval($number); # This fixes a stupid computer math problem.
push (@output_array, $number);
$number = $number - $3;
}
} else {
my $number = $1;
while ($number <= $2) {
next if grep(/^$number$/, @excludes);
$number = eval($number); # This fixes a stupid computer math problem.
push (@output_array, $number);
$number = $number + $3;
}
}
} else {
print STDERR "Invalid expression: $expression\n" if ($verbose >= 1);
}
}
my $i = 0;
my $max = @output_array;
while ($i < $max) {
if ($i == ($max - 1)) {
print $prefix . $output_array[$i] . $suffix; # No need to put a separator for the last element.
} else {
print $prefix . $output_array[$i] . $suffix . $separator;
}
$i++;
}
print "\n";
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
--------------------------------------------------------------------
range : Print out a range of numbers for use in for loops and such.
--------------------------------------------------------------------
Usage:
range [options] //
Options:
-e Exclude the of numbers from the range output.
is a set of numbers separated by commas.
-n Use as a separator between numbers. By default, it
is a space, use '\n' or \\n for newline or the -N option.
-N Just a quick option for using a newline as the separator.
-p Specify a prefix to use for every number output.
-s Specify a suffix to use for every number output.
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
Expressions:
/1..100/ All numbers from 1 to 100.
/1..50,60..75/ Multiple ranges.
/0..100i2/ All even numbers from 0 to 100.
/1..100i2/ All odd numbers from 1 to 100.
/3..100i3/ Factors of 3.
/1.1..4.5i0.1/ Decimal ranges.
EOF
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
range - Print out a range of numbers for use in for loops and such.
=head1 SYNOPSIS
B [-dhV] //
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
will print out a list of numbers based on an expression that you specify. This
is useful for making a list of numbers for use in for loops and so on. Ranges of
numbers are specified using the .. operator, like this /20..50/, which means all
integers from 20 and 50. More complex expressions can be generated using the commas
and the 'i' increment operator.
=head1 OPTIONS
-e Exclude the of numbers from the range output.
is a set of numbers separated by commas.
-n Use as the separator between numbers. By default, it
will use a space. Use '\n' or \\n for a newline character or
use the -N option.
-N Just a quick option for using a newline as the separator.
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
=head1 EXAMPLES
All numbers from 1 to 10.
$ range /1..10/
1 2 3 4 5 6 7 8 9 10
From 10 to 1. Counting down.
$ range /10..1/
10 9 8 7 6 5 4 3 2 1
From 1 to 10 and from 15 to 20.
$ range /1..10,15..20/
1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 20
Even numbers from 0 to 10
$ range /0..10i2/
0 2 4 6 8 10
Odd numbers. Notice the starting number in the range expression.
$ range /1..10i2/
1 3 5 7 9
Factors of 3 between 99 and 120.
$ range /99..120i3/
99 102 105 108 111 114 117 120
Decimal numbers
$ range /1.1..2.5i0.1/
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5
And negative numbers too.
$ range /1.0..-2.0i0.3/
1 0.7 0.4 0.1 -0.2 -0.5 -0.8 -1.1 -1.4 -1.7 -2
You can also pad numbers when you are counting up. This is
a trick of how the Perl programming language deals with ranges:
$ range /01..15/
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
=head1 BUGS
Even though you can do zero padding on simple ranges, like 001..100,
it will not pad zeros on complex ranges like 001..100i2, or for counting
downwards.
=head1 SEE ALSO
seq(1), average(1), bound(1), interval(1), normalize(1), numgrep(1), numprocess(1), numsum(1), random(1), round(1)
=head1 COPYRIGHT
range 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
submitions or help for the project.
=head1 MORE INFO
More info on range can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/random0100775000076400007640000001435010147276245013313 0ustar susosuso#!/usr/bin/perl -w
# random: Print out a random number.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
my ($expression, @random_array, $number);
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 $range_expression = shift || "/1..100/"; # Give a default expression
# if none are specified.
$range_expression =~ s/^\///;
$range_expression =~ s/\/$//;
my @range_expressions = split(/,/, $range_expression);
################
# MAIN PROGRAM #
################
# This part is almost exactly the same as the range program, except
# for the last part of course.
my $number_exp = "-?[0-9]*\\.?[0-9]+";
foreach $expression (@range_expressions) {
print "expression: $expression\n" if ($verbose >= 3);
if ($expression =~ /^($number_exp)$/) {
push @random_array, $expression;
} elsif ($expression =~ /^($number_exp)\.\.($number_exp)$/) {
print "1: $1\n2: $2\n" if ($verbose >= 3);
if ($1 > $2) {
my $number = $1;
while ($number >= $2) {
push (@random_array, $number);
$number--;
}
} else {
my $number;
foreach $number ($1..$2) {
push (@random_array, $number);
}
}
} elsif ($expression =~ /^($number_exp)\.\.($number_exp)i($number_exp)$/) {
print "1: $1\n2: $2\n3: $3\n" if ($verbose >= 3);
if ($1 > $2) {
my $number = $1;
while ($number >= $2) {
$number = eval($number); # This fixes a stupid computer math problem.
push (@random_array, $number);
$number = $number - $3;
}
} else {
my $number = $1;
while ($number <= $2) {
$number = eval($number); # This fixes a stupid computer math problem.
push (@random_array, $number);
$number = $number + $3;
}
}
} else {
print STDERR "Invalid expression: $expression\n" if ($verbose >= 1);
}
}
my $random_array_max = @random_array;
my $rand_value = int(rand($random_array_max));
my $random_number = $random_array[$rand_value];
print "$random_number\n";
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
-------------------------------------
random : Print out a random number.
-------------------------------------
Usage:
random [options] /[expression]/
Options:
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
Expressions:
/1..100/ Random number from 1 to 100.
/1..50,60..75/ Random number from multiple ranges.
/0..100i2/ Random even number from 0 to 100.
/1..100i2/ Odd number from 1 to 100.
/0..100i3/ Random factor of 3 from 0 to 100.
/1.1..4.5i0.1/ Decimal range.
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $number_array_ref = shift;
while (<$filehandle>) {
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
print STDERR "found number: $1\n" if ($verbose >= 3);
push(@$number_array_ref, $1);
}
}
return 1;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
random - Print out a random number.
=head1 SYNOPSIS
B [-dhV] /[expression]/
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
will print out a random number determined by the expression that you give. The
syntax and program is nearly identical to the B program, except that B
picks a number at random from the range expression. If no expression is specified,
B will print out a random integer between 1 and 100.
=head1 OPTIONS
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
=head1 EXAMPLES
Random number from 1 to 10.
$ random /1..10/
7
From 1 to 10 or from 15 to 20.
$ random /1..10,15..20/
16
An even number from 0 to 10
$ random /0..10i2/
4
An odd number. Notice the starting number in the range.
$ random /1..10i2/
9
A factor of 3 between 99 and 120.
$ random /99..120i3/
111
A decimal number.
$ random /1.1..2.5i0.1/
1.8
A negative random number.
$ random /0.0..-2.0i0.3/
-0.8
=head1 BUGS
B is slow when dealing with large ranges to randomly
find a number from. This is because it creates a list of all
potential numbers before picking one. So it can be memory intensive
for large ranges.
=head1 SEE ALSO
average(1), bound(1), interval(1), normalize(1), numgrep(1), numprocess(1), numsum(1), range(1), round(1)
=head1 COPYRIGHT
random 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
submitions or help for the project.
=head1 MORE INFO
More info on random can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/bound0100775000076400007640000001173510147276245013146 0ustar susosuso#!/usr/bin/perl -w
# bound: Find boundary numbers in files or STDIN.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
my ($file, @number_array);
getopts('dhlV', \%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.
}
################
# 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, \@number_array) ||
$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, \@number_array);
}
my $final_number;
if ($opts{'l'}) {
$final_number = find_lower_bound(\@number_array);
} else {
$final_number = find_upper_bound(\@number_array);
}
print "$final_number\n";
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
-------------------------------------------------------------------------------
bound : Find boundary numbers in files or STDIN. By default, find the maximum.
-------------------------------------------------------------------------------
Usage:
bound [options]
| bound [options]
bound [options]
Options:
-l -- Return the lower bound number in the set (the minimum number)
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $number_array_ref = shift;
while (<$filehandle>) {
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
print STDERR "found number: $1\n" if ($verbose >= 3);
push(@$number_array_ref, $1);
}
}
return 1;
}
sub find_upper_bound {
my $number_array_ref = shift;
my $highest_number = @$number_array_ref[0];
my $number;
foreach $number (@$number_array_ref) {
if ($number > $highest_number) {
$highest_number = $number;
}
}
return $highest_number;
}
sub find_lower_bound {
my $number_array_ref = shift;
my $lowest_number = @$number_array_ref[0];
my $number;
foreach $number (@$number_array_ref) {
if ($number < $lowest_number) {
$lowest_number = $number;
}
}
return $lowest_number;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
bound - Find boundary numbers in files or STDIN.
=head1 SYNOPSIS
B [-dhlV]
| B [-dhlV] (Input on STDIN from pipeline.)
B [-dhlV] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
will find boundary numbers (minimum and maximum) in files or STDIN. By default
it will find the upper bound in the set of numbers (the maximum number) in the
files or on STDIN. You can use the -l option for finding the lower bound (minumum
number).
=head1 OPTIONS
-l -- Return the lower bound number in the set (the minimum number)
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
=head1 BUGS
B currently will only gather the first number on each line instead of
all the numbers on the lines.
=head1 SEE ALSO
average(1), interval(1), normalize(1), numgrep(1), numprocess(1), numsum(1), random(1), range(1), round(1)
=head1 COPYRIGHT
bound 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
submitions or help for the project.
=head1 MORE INFO
More info on bound can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/numprocess0100775000076400007640000001566310147276245014241 0ustar susosuso#!/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 .
#######################
# VARIABLES AND SETUP #
#######################
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] //
| numprocess [options] //
numprocess [options] //
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
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;
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)$/) {
$new_number = $new_number / $1;
} elsif ($operation =~ /^\^($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 [-dhV] // [FILE or STDIN]
| B [-dhV] // (Input on STDIN from pipeline.)
B [-dhV] // (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
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 celcius to fahreheit 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
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
submitions or help for the project.
=head1 MORE INFO
More info on numprocess can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/tests/0040775000076400007640000000000010147276317013247 5ustar susosusonum-utils-0.5/tests/columns20100664000076400007640000000015710147276246014735 0ustar susosuso1 a 6 11 16 c2t 21
2 b 7 12 17 d0g 22
3 c 8 13 18 33 23
4 d 9 14 19 7ark 24
5 e 10 15 20 m0us3 25
num-utils-0.5/tests/README0100664000076400007640000000017607503214754014127 0ustar susosuso
These are tests that I use to make sure that the numeric utilities
are working and also to demonstrate their abilities.
num-utils-0.5/tests/columns0100664000076400007640000000041510147276246014650 0ustar susosuso1 6 11 16 21 26 31 36 41 46 51
2 7 12 17 22 27 32 37 42 47 52 57 62 67 72
3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88 93 98 103
4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89 94 99 104
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105
num-utils-0.5/tests/numbers20100664000076400007640000000051307401755122014716 0ustar susosuso5
135
135
1 1.5 -4.5351
3
67 245.424
3
7
9
190 0 45 2351 4345.2341.3515 3515.315
3
24FKAF2451.4141A4.124124
2542
Hi, my name is Logan-5. Some people just called me Logan5 though 4 obvious
r3as0ns-like going to the ma11. Ch-3-ck th1-s -0ut.
-.-.---..---.235415.-.--.35-.35-.13--5.3616---315135--35-35.15.35.15-35135-----
num-utils-0.5/tests/file0100664000076400007640000000150107401755122014076 0ustar susosusols: /var/spol/mail: No such file or directory
total 3016
-rw-r--r-- 1 root root 253052 Dec 7 07:20 boot.log
-rw------- 1 root root 232098 Dec 7 07:50 cron
-rw-r--r-- 1 root root 2445 Dec 7 07:05 dmesg
-rw-r--r-- 1 root root 0 Jan 7 1980 htmlaccess.log
-rw-r--r-- 1 root root 1184060 Dec 7 07:06 lastlog
-rw------- 1 root root 15107 Dec 7 07:20 maillog
-rw------- 1 root root 1377202 Dec 7 07:37 messages
-rw-r--r-- 1 root root 479 Nov 25 10:31 netconf.log
-rw------- 1 root root 7356 Dec 7 07:06 secure
-rw-r--r-- 1 root root 616 Dec 7 04:03 sendmail.st
-rw------- 1 root root 0 Jan 7 1980 spooler
-rw-rw-r-- 1 root utmp 1133184 Dec 7 07:06 wtmp
num-utils-0.5/tests/numbers0100664000076400007640000000020207474335006014634 0ustar susosuso5
135
135
1 1.5 4.5351
3
67 245.424
3
46 99
9
47 30
-1
-2.543
7
9 -10 -1
90 45 -315.1338571535 -797.325 -1
3
5135 2
8
num-utils-0.5/tests/fractionalnums0100664000076400007640000000030307502661632016207 0ustar susosuso1.2
3.45 335.089735
3.0
8.5153135 5315135.4535987 -5.193857 -6.93951
10241 350981.1358735 319507.13571375 35135 -13975.1357103597
8.2 4.5 7.8 9.3
-10.5
-4.44999
-0.4999999
-0.65315
-0.50000000
num-utils-0.5/tests/zeros0100664000076400007640000000014507502661632014330 0ustar susosuso-0.45
-000.111111
0.4
0.6
0.88888
-0.9899898
00000.4321
-00000
000
.3515
-.3515
0001.3515
-0001.3515
num-utils-0.5/interval0100775000076400007640000001100410147276245013650 0ustar susosuso#!/usr/bin/perl -w
# interval: This program calculates and displays the intervals between the
# first line and the second, between the second line and the third on
# through the end of the file.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
getopts('hdVq', \%opts);
my ($file, @number_array,$interval, $number);
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.
}
################
# 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, \@number_array) ||
$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, \@number_array);
}
my $lastnumber;
foreach $number (@number_array) {
if ($lastnumber) {
$interval = $number - $lastnumber;
print "$interval\n";
}
$lastnumber = $number;
}
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
-----------------------------------------------------------------------
interval : Calculate the numeric intervals between lines in input.
-----------------------------------------------------------------------
Usage:
template [options]
| template [options]
template [options]
Options:
-q Don't print any warnings.
-d Debug. For developers only.
-V Increase verbosity.
-h Help: You're looking at it.
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $number_array_ref = shift;
while (<$filehandle>) {
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
print STDERR "found number: $1\n" if ($verbose >= 3);
push(@$number_array_ref, $1);
}
}
return 1;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
interval - Show the numeric intervals between each line in a file.
=head1 SYNOPSIS
B [-dhV]
| B [-dhV] (Input on STDIN from pipeline.)
B [-dhV] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
will calculate and display the numeric interval between one number and the
next on an input stream.
This can be quite useful for showing quantities of increase or decrease
of data when the numbers themselves aren't providing you with what you want.
For instance, I wrote this program after wanting to see the rate of change
in box office ticket sales for movies on imdb.com. ;-)
=head1 OPTIONS
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
-q Don't print any warnings. Quiet mode.
=head1 SEE ALSO
average(1), bound(1), normalize(1), numgrep(1), numprocess(1), numsum(1), random(1), range(1), round(1)
=head1 COPYRIGHT
interval 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
submitions or help for the project.
=head1 MORE INFO
More info on template can be found at:
=over 1
=item B
=back
=cut
num-utils-0.5/normalize0100775000076400007640000001167210147276245014037 0ustar susosuso#!/usr/bin/perl -w
# normalize: Normalize a set of numbers. By default between 0 and 1.
#
# 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 .
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
my ($file, @number_array, @output_array, $number);
getopts('hdR:V', \%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.
}
################
# 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, \@number_array) ||
$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, \@number_array);
}
@output_array = normalize(\@number_array);
foreach $number (@output_array) {
print "$number\n";
}
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
----------------------------------------------
normalize : Normalize a set of numbers.
----------------------------------------------
Usage:
normalize [options]
| normalize [options]
normalize [options]
Options:
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
-R This allows you to specify a different
normalized range instead of from 0 to 1.
For example: -R 0..5
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $number_array_ref = shift;
while (<$filehandle>) {
if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
print STDERR "found number: $1\n" if ($verbose >= 3);
push(@$number_array_ref, $1);
}
}
return 1;
}
sub normalize {
my $number_array_ref = shift;
my ($sum, @normalized_array);
my ($top_range, $bottom_range);
if ($opts{'R'}) {
($bottom_range,$top_range) = split(/\.\./, $opts{'R'});
print "R: $opts{'R'}\nbottom: $bottom_range\ntop: $top_range\n" if ($verbose >= 2);
} else {
$bottom_range = 0;
$top_range = 1;
}
foreach $number (@$number_array_ref) {
$sum += $number;
}
foreach $number (@$number_array_ref) {
my $normalized_number = ($number / $sum) * ($top_range - $bottom_range) + $bottom_range;
push(@normalized_array, $normalized_number);
}
return @normalized_array;
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
normalize - Normalize a set of numbers. By default between 0 and 1.
=head1 SYNOPSIS
B [-dhRV]
| B [-dhRV] (Input on STDIN from pipeline.)
B [-dhRV] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B is a program that is part of the numeric utilities package. B
will take a set of numbers on input and return that set as a normalized set of numbers
between 0 and 1 by default. Or you can use the -R option to specify a different normalized
range.
=head1 OPTIONS
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
-R This allows you to specify a different normalized range instead of from 0 to 1.
For example -R 0..5
=head1 SEE ALSO
average(1), bound(1), interval(1), numgrep(1), numprocess(1), numsum(1), random(1), range(1), round(1)
=head1 COPYRIGHT
normalize 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
submitions or help for the project.
=head1 MORE INFO
More info on normalize can be found at:
=over 1
=item B
=back
=cut