# check for exact # of digits

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I have this at the beginning of a sub (\$checkDate is an input parameter).
I want to check for exactly 8 digits.
This works for less than 8 but doesn't work for more than 8.

if (\$checkDate !~ /^\d/) {
return "Date (\$checkDate) must be YYYYMMDD\n";
}

## Re: check for exact # of digits

You should anchor (with \$) the end of the string as well:
if (\$checkDate !~ /^\d\$/) {

## Re: check for exact # of digits

On 08/25/2012 12:40 PM, Klaus wrote:

If you want to check for *exactly* 8, also leave out the "1,":
if (\$checkDate !~ /^\d\$/) {

You could also check *and* split:
if (my (\$y, \$m, \$d) = (\$checkdate =~ /^(\d)(\d)(\d)\$/)) {
print "\$d.\$m.\$y\n";
}

Josef

## Re: check for exact # of digits

Be a little careful with \d, \s and \w. If there is any chance your
strings might contain Unicode characters, they will match a lot more
than you might expect. If you have perl 5.14, you can avoid this with
the /a modifier; otherwise, I would recommend using [0-9] instead.

Ben

## Re: check for exact # of digits

bjlockie wrote:

if ( 8 == \$checkDate =~ tr/0-9// ) {
return "Date (\$checkDate) must be YYYYMMDD\n";
}

John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

## Re: check for exact # of digits

Quoth jwkrahn@shaw.ca:

That's not the same. For one thing I believe you have the condition the
wrong way around; for another, the OP's pattern is anchored (at the
beginning, and should be at the end), which cannot be emulated with
tr///.

Something along the lines of

unless (length \$checkDate == 8 and \$checkDate =~ tr/0-9// == 8) {

or

if (length \$checkDate != 8 or \$checkDate =~ tr/0-9//c) {

might work, but is more cumbersome than a pattern match.

Ben

## Re: check for exact # of digits

Ben Morrow wrote:

Duh!

No, it is correct.

The OP should have been more explicit in their specification.

"I want to check for exactly 8 digits"

Which is accomplished by my solution.

John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

## Re: check for exact # of digits

[...]

[...]

As Robert Pike once quipped: If the program compiles, the machine is
happy. For that matter,

if (abs(sqrt(64) - \$checkdate =~ tr/0-9//) <= 0.05)

would just be as correct. And it even also avoids having an lvalue on
the left side of ==.

## Re: check for exact # of digits

I don't see how this "works for less than 8 but doesn't work for more
than 8". It accepts "1234567" just as it accepts "123456789".

No. Your code complains if \$checkDate contains 8 digits and accepts all
other formats:

| jwkrahn: Date (12345678) must be YYYYMMDD

which is obviously the opposite of what the OP was trying to accomplish.

That's not the whole specification. You forgot:

"Date (\$checkDate) must be YYYYMMDD\n";

The sentence you quoted didn't specify whether the string should consist
of 8 digits or contain 8 digits. If that string was the only information
you had, both interpretations might be valid. But you had more
information and deliberately chose the one which was contradicted by the
additional information. (I'm not sure whether you also deliberately
inverted the consequence of the check).

hp

--
_  | Peter J. Holzer    | Deprecating human carelessness and
|_|_) | Sysadmin WSR       | ignorance has no successful track record.
| |   | hjp@hjp.at         |
__/   | http://www.hjp.at/ |  -- Bill Code on asrg@irtf.org

## Re: check for exact # of digits

Please do not copy this style. The justification for that is that -
occasionally - this will result in a compiler error when some
mathematically inclined newbie forgot that = is the assignment
operator and not the one which tests for equality. But in reality,
it's just another stylistic byzantinism[*] some people love to use
because the very notion of doing anything in straight-forward way just
feels wrong to them.

[*] In real languages, one asks for the value of an attribute (Is the
color red?) and not for the attribute of a value (Is red the color?,
aka 'She is your sister' vs 'Your sister she is').

## Re: check for exact # of digits

Rainer Weikusat wrote:
)>    if ( 8 == \$checkDate =~ tr/0-9// ) {
)
) Please do not copy this style. The justification for that is that -
)  <snip>
)
) [*] In real languages, one asks for the value of an attribute (Is the
) color red?) and not for the attribute of a value (Is red the color?,
) aka 'She is your sister' vs 'Your sister she is').

Perhaps we should call that Yoda-style, from now on. :-)

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

## Re: check for exact # of digits

rweikusat@mssgmbh.com says...

Funny (and true)...

John Black