in reply to Re: How to determine a variable value is a number
in thread How to determine a variable value is a number
Does this pass muster for zero, signed, and float type numbers?#!/usr/bin/perl -w use strict; sub i_am_a_number { shift; no warnings; # += 0 on a string sets off a warning # make sure that zero is counted as a number # ($_ += 0) alone fails on zero. if (/^\d+$/ || ($_ += 0)) { return 1; } return 0; } ############################################################# # take the sub on a test spin my @list = (035, 35, +19, 12, "045", -2, 5.5, 0, -34.530, "hi!"); for (@list) { if (i_am_a_number($_)) { print "$_ is a number!\n"; } }
()-() \"/ `
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re3: How to determine a variable value is a number
by blakem (Monsignor) on Sep 19, 2002 at 11:07 UTC | |
by ignatz (Vicar) on Sep 19, 2002 at 11:48 UTC |