RE: PERL: How to Count Numeric Digits

by "Brian A. Sayrs" <sowinso(at)sowinso.com>

 Date:  Sun, 15 Oct 2000 09:04:01 -0700
 To:  "'david(at)IslandTime.com'" <david(at)IslandTime.com>,
"hwg-languages(at)hwg.org" <hwg-languages(at)hwg.org>
  todo: View Thread, Original
There are several ways, some more painful than others...

METHOD ONE:  You can split the string into an array of characters which =
you check individually:
@array =3D split(//, $phonestring);


METHOD TWO: But since you have to run a loop to check to see if it's a =
digit, you might as well do this:
	$count=3D0;
while ($phonestring =3D~ /(\d)/g) #only enters the loop if the character =
in question is a digit
{
	#here, $1 contains the value of the digit in question, but since we're =
just
	#counting, simply ignore what's in $1, and increment our count variable
        		++$count;
    	}
	#count holds the number of digits in the string


METHOD THREE: Of course, there's also a generalized regular expression =
which matches North American phone numbers, but this method doesn't fit =
your because it doesn't ignore everything unusual, and if it were to =
handle everything, it would be hard to understand.

Anyway, I double checked the second method, and it works fine.  It has =
an additional advantage in that you could also reformat the phone number =
in any way you'd want.

Just as an aside, another non-perl thing you can do is limit the size of =
the input field.  With your form, put this in:
<.input type=3D"text" name=3D"phonenumber" maxlength=3D"10">

This way, if you're requiring area code and phone number, your users =
will quickly figure out that there isn't supposed to be any punctuation, =
just numbers.  The disadvantage is that it might raise their blood =
pressure a bit, if this is a complaint page...  :)

Brian A. Sayrs
Owner, Southwind Solutions



-----Original Message-----
From:	david(at)IslandTime.com [SMTP:david(at)IslandTime.com]
Sent:	Saturday, October 14, 2000 9:18 PM
To:	hwg-languages(at)hwg.org
Subject:	PERL: How to Count Numeric Digits=20

Hi Folks,

Would someone have the time to provide me with a "simple" (easy to=20
understand) expression in PERL that will count the number of digits in a =

text variable.

Specifically, I'm trying to determine that a Phone Number field passed =
from=20
a form contains exactly 10 numeric digits ... ignoring any and all=20
parenthesis, dashes, and spaces that may exist in the field.

Thanks in advance for any assistance,

david=20

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA

This page is part of a preserved archive of archives.hwg.org. The site is no longer active and its content is not maintained. For enquiries about this archive, write to archive(at)iwanet.org.