Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: (Buzzcutbuddha - Pack is an alternative) Validating Numbers in CGI Script?

by buzzcutbuddha (Chaplain)
on May 11, 2001 at 00:34 UTC ( [id://79552]=note: print w/replies, xml ) Need Help??


in reply to Validating Numbers in CGI Script?

This solution might not be optimal (as it is not very strict and not exactly what you want) but you could also use pack and unpack.

As you can see from the code below, I tested the eval, the regex posted by japhy, and two versions of pack,
one that stores the number as a double precision float (packstuffD), and one that stores the number as a single precision float (packstuffF).

The regex outputs:
4.34 -2.33333 5 20
Whereas both of the packs output:
4.34 -2.33333 0 5 0 5 12 0 0 0 20 4.3e+015

As you can see, anything that is not a number gets turned into a zero by the pack/unpack process, which is good
and harmless, and only returns valid Perl numbers which is not quite what you wanted
so this became was a fun little exercise for myself!
use Benchmark; sub regexstuff() { map {$foo = $_ if /^-?(?=\d|\.\d)\d*\.?\d+$/} (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub packstuffD() { map { $foo = unpack ("d", pack ("d", $_)), "\n" } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub packstuffF() { map { $foo = unpack ("f", pack ("f", $_)), "\n" } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub evalstuff() { map { eval{ local $^W = 1; $_ + 0 } } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); if ( $@ ) { $result = "$invalue is *not* a Number; Details: $@" } else { $result = "$invalue appears to be a number."; } } timethese ( 100000, { packstuffD => "packstuffD", regex => "regexstuff", eval => "evalstuff", packstuffF => "packstuffF" } ); eval: 13 wallclock secs (11.97 usr + 0.02 sys = 11.99 CPU) @ 83 +42.37/s (n=100000) packstuffD: 11 wallclock secs (10.06 usr + 0.00 sys = 10.06 CPU) @ 99 +45.30/s (n=100000) packstuffF: 10 wallclock secs (10.63 usr + 0.02 sys = 10.65 CPU) @ 93 +93.20/s (n=100000) regex: 11 wallclock secs (11.01 usr + 0.01 sys = 11.02 CPU) @ 90 +77.71/s (n=100000)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found