in reply to need idiom - compare a number to a string
Since Perl will autostringify as needed, just check to see if $value looks like a number or not. I'd probably do something radically sub-optimal like
$value = lookupString($value) if($value !~ /^\d+$/);
I'm sure one of the monks who has Perl skills above mine (i.e. almost everybody) has a better way, but I think my fragment will avoid too many errors (I'd probably demonstrate paranoia by checking to see if $value is defined before the match)
which should deal with signed and unsigned integers, but will reject cases where $value includes a radix point or is in exponential notation.$value =~ /^\s*[+-]?\s*\d+\s*$/
emc
" The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents. " — Nathaniel S. Borenstein
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: need idiom - compare a number to a string
by chromatic (Archbishop) on Mar 17, 2006 at 23:19 UTC | |
by ikegami (Patriarch) on Mar 18, 2006 at 00:08 UTC |