[VPM] detecting non-integers

Malcolm Dew-Jones yf110 at victoria.tc.ca
Mon May 5 18:47:33 CDT 2003




On Mon, 5 May 2003, Carl B. Constantine wrote:

> Since perl doesn't have types as such, what is the best way to detect
> this, just a regex like: =~/\d+(\.)*/ or is there an easier way?

A regex is the most typical way.  I would show some but I hate to do that
without testing them cause it's so easy for something to not work quite as
you expect.  Well may be just one, the simplist

	m/^\d+$/;	# only digits, and must be at least one of them


I suspect that there is a page somewhere that has pre-canned regexes, but
I'm afraid I don't have an address.

An alternative is to accept anything and convert it to the type of number
you want.  If the user sees feedback then that can work fine also.  (by
"type" I mean things like a truncated integer, or a number rounded to some
number of places, or extract a numeric portion, etc). 

	$number = sprintf "%d" , $input ; # non-numeric gives 0

	or

	($number) = $input =~ m/(\d+)/;   # extract an embedded number
	                                  # good for "50 kgs" etc






More information about the Victoria-pm mailing list