App-pltest
view release on metacpan or search on metacpan
pod/examples.pod view on Meta::CPAN
for dim & bright and for background & foreground. Hence the multiplication of
escape codes and of values to fill them.
This fills C<@A(RGV)> in B<-A> twice, the 2nd time looping over the 1st list,
as though it had been given on the command line. It maps it to the 16-fold
number format to print the header, swallowing every other number with 0-width.
Then the main program loops over it pairwise with C<$A(RGV)>, thanks to
B<-o2>, to print the body. Numbers are duplicated with C<(N)x2>, once to go
into the escape sequence, once to be displayed:
pltest -o2A 1..8 -A '$_, $_+39, $_+8, $_+99' -B 'form "co: bg;fg"."%4d%.0s"x16, @ARGV;
$b = Form "\e[%dm%3d "x16, map +(($_)x2, ($_+60)x2), 30..37' \
'form "%2d: %4d; \e[%2:dm$b\e[m", @$_'
=item Terminal Rulers
If you need a ruler to better count the width of some other output, you can
print out one of the following. These are either decimal, by groups of 5 or
hex by groups of 4 and 8. The latter two do the same. But instead of one
static ruler of length 100 or 256, they repeat and adapt. Depending on your
terminal emulator and Shell, the variable C<$COLUMNS> may track the current
width. If so, pass it in a single argument to "loop" over with B<-o>, else
provide the desired width yourself:
pltest 'say map "$_...:.....", 0..9'
pltest 'say map "$_..:...|...:....", 0..9, "A".."F"'
pltest -o 'say substr join( "", map "$_...:.....", 0..9 ) x ($_ / 100 + 1), 0, $_' $COLUMNS
pltest -o 'say substr join( "", map "$_..:...|...:....", 0..9, "A".."F" ) x ($_ / 256 + 1), 0, $_' $COLUMNS
=back
=head2 Math
=over
=item Minimum and Maximum
The C<List::Util> functions C<min> and C<max> are imported for you:
pltest 'echo max 1..5'
If you have just several numbers on each line and want their minimums, you can
autosplit (B<-a>) to C<@F(IELD)>:
pltest -a 'echo min @FIELD' file*
If on the same you just want the overall minimum, you can use the print-at-end
variable C<$R(ESULT)>, which you initialise to infinity in a B<-B> begin
program:
pltest -aB '$RESULT = "inf"' '$RESULT = min $RESULT, @FIELD' file*
Likewise for overall maximum, you start with negative infinity:
pltest -aB '$RESULT = "-inf"' '$RESULT = max $RESULT, @FIELD' file*
=item Median, Quartiles, Percentiles
The median is the number where half the list is less and half is greater.
Similarly the 1st quartile is where 25% are less and the 3rd where 25% are
greater. Use a list slice to extract these 3 and a 97th percentile, by
multiplying the fractional percentage with the list length:
pltest -A 0..200 'echo @ARGV[map $_*@ARGV, .25, .5, .75, .97]'
If you'd rather have names associated, assign them to hash slice in
sort-numerically-at-end C<%N(UMBER)>, whose key order must match the
percentages:
pltest -A 0..200 '@NUMBER{qw(lower median upper 97.)} = @ARGV[map $_*@ARGV, .25, .5, .75, .97]'
=item Triangular Number, Factorial and Averages
I<80% of people consider themselves to be above average. :-)>
The triangular number is defined as the sum of all numbers from 1 to I<n>, e.g. 1
to 5. Factorial is the equivalent for products:
pltest 'echo sum 1..5;
echo product 1..5'
The sum of all list elements divided by the length of the list gives the
linear average. Alternately the root mean square can be a fairer average,
because it accounts for the weights:
pltest -A 11..200 'echo sum( @ARGV ) / @ARGV;
echo sqrt sum( map $_ ** 2, @ARGV ) / @ARGV'
=item Add Pairs or Tuples of Numbers
If you have a list of number pairs and want to add each 1st and each 2nd
number, C<reduce> is your friend. Inside it map over the pair elements
C<0..1>:
pltest 'echo reduce {
[map $a->[$_] + $b->[$_], 0..1]
} [1, 11], [2, 12], [3, 13]'
If your list is a variable and is empty the result is C<undef>. You can
insert a fallback zero element if you'd rather receive that for an empty list:
pltest 'echo reduce {
[map $a->[$_] + $b->[$_], 0..1]
} [0, 0], @list'
The above adds pairs, because we iterate C<0..1>. This can be generalized to
tuples by iterating to the length of the 1st array:
pltest 'echo reduce {
[map $a->[$_] + $b->[$_], 0..$#$a]
} [1, 11, 21], [2, 12, 22], [3, 13, 23]'
=item Big Math
I<2 + 2 = 5 for extremely large values of 2. :-)>
With the C<bignum> and C<bigrat> modules you can do arbitrary precision and
semi-symbolic fractional math:
pltest -Mbignum 'echo 123456789012345678901234567890 * 123456789012345678901234567890'
pltest -Mbignum 'echo 1.23456789012345678901234567890 * 1.23456789012345678901234567890'
pltest -Mbigrat 'echo 1/23456789012345678901234567890 * 1/23456789012345678901234567890'
=item Primes
This calculates all primes in a given range, e.g. 2 to 99:
( run in 0.702 second using v1.01-cache-2.11-cpan-39bf76dae61 )