Just another take:
#!perl
use strict;
# BrowserUK's function defs
my %funcs = (
power => sub { return exp( $_[0] ) },
log2 => sub { return log( $_[0] )/log(2.0) },
loge => sub { return log( $_[0] ) },
log10 => sub { return log( $_[0] )/log(10.0) },
round => sub { return int( $_[0] + $_[ 0 ] < 0 ? -0.5 : 0.5 ) },
trunc => sub { return int($_[0]) },
f1 => sub { return sprintf "%.1f", $_[0] },
f2 => sub { return sprintf "%.2f", $_[0] },
f3 => sub { return sprintf "%.3f", $_[0] },
f4 => sub { return sprintf "%.4f", $_[0] },
f5 => sub { return sprintf "%.5f", $_[0] },
);
my $A = shift;
my $chosen = $funcs{$A} or die "No such func $A\n";
s/(?<=\s)(\S+)/$chosen->($1)/ge, print while <>;
Update: changed from \w to \S.
Caution: Contents may have been coded under pressure.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.