Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: 4x faster now! (updated)

by AnomalousMonk (Archbishop)
on Jul 19, 2018 at 21:50 UTC ( [id://1218855]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 4x faster now! (updated)
in thread Syntax Perl Version support $c = () = $a =~ /\./g

Just out of curiosity, a couple more variations. Note that Scalar::Util::looks_like_number() thinks things like  +1  -1  2e3 look like numbers that you may not want to accept.

c:\@Work\Perl\monks>perl -wMstrict -le "use Scalar::Util qw(looks_like_number); ;; use Data::Dump qw(pp); ;; for my $n ( undef, '', '.', '..', '.1.', '1..1', 0, '0', '0.', '0.0', '.0', '000.000', 1, '1', '1.', '1.0', '001.100', '+1', '-1', '2e3', ) { printf qq{%s %s like num \n}, pp($n), looks_like_number($n) ? 'looks' : 'does not look'; } " undef does not look like num "" does not look like num "." does not look like num ".." does not look like num ".1." does not look like num "1..1" does not look like num 0 looks like num 0 looks like num "0." looks like num "0.0" looks like num ".0" looks like num "000.000" looks like num 1 looks like num 1 looks like num "1." looks like num "1.0" looks like num "001.100" looks like num "+1" looks like num -1 looks like num "2e3" looks like num c:\@Work\Perl\monks>perl -wMstrict -le "sub unsigned_real { return defined($_[0]) && $_[0] =~ m{ \A (?: \d+ (?: [.] \d*)? | \d* [.] \d+) \z }xms ; } ;; use Data::Dump qw(pp); ;; for my $n ( undef, '', '.', '..', '.1.', '1..1', '+1', '-1', '2e3', 0, '0', '0.', '0.0', '.0', '000.000', 1, '1', '1.', '1.0', '001.100', ) { printf qq{%s %s real \n}, pp($n), unsigned_real($n) ? 'looks' : 'does not look'; } " undef does not look real "" does not look real "." does not look real ".." does not look real ".1." does not look real "1..1" does not look real "+1" does not look real -1 does not look real "2e3" does not look real 0 looks real 0 looks real "0." looks real "0.0" looks real ".0" looks real "000.000" looks real 1 looks real 1 looks real "1." looks real "1.0" looks real "001.100" looks real
If the speed of any of these is acceptable, you will, of course, develop your own Test::More test suite(s). (But you'll want to do | you've already done that in any case. :)


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1218855]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found