check for exact # of digits

B

bjlockie

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{1,8}/) {
return "Date ($checkDate) must be YYYYMMDD\n";
}
 
K

Klaus

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{1,8}/) {
        return "Date ($checkDate) must be YYYYMMDD\n";
   }

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

Josef Moellers

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

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

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

Josef
 
J

John W. Krahn

bjlockie said:
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{1,8}/) {
return "Date ($checkDate) must be YYYYMMDD\n";
}

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



John
 
R

Rainer Weikusat

John W. Krahn said:
if ( 8 == $checkDate =~ tr/0-9// ) {

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').
 
W

Willem

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
 
J

John W. Krahn

Ben said:
Quoth (e-mail address removed):

That's not the same.
Duh!

For one thing I believe you have the condition the
wrong way around;

No, it is correct.
for another, the OP's pattern is anchored (at the
beginning, and should be at the end), which cannot be emulated with
tr///.

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
 
R

Rainer Weikusat

John W. Krahn said:
Ben said:
Quoth (e-mail address removed):
[...]
[...]
For one thing I believe you have the condition the
wrong way around;

No, it is correct.

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 ==.
 
P

Peter J. Holzer

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, it is correct.

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.

The OP should have been more explicit in their specification.

"I want to check for exactly 8 digits"

That's not the whole specification. You forgot:

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

Which is accomplished by my solution.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top