check for exact # of digits

Discussion in 'Perl Misc' started by bjlockie, Aug 25, 2012.

  1. bjlockie

    bjlockie Guest

    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";
    }
    bjlockie, Aug 25, 2012
    #1
    1. Advertising

  2. bjlockie

    Klaus Guest

    On 25 août, 11:56, bjlockie <> wrote:
    > 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}$/) {
    Klaus, Aug 25, 2012
    #2
    1. Advertising

  3. On 08/25/2012 12:40 PM, Klaus wrote:
    > On 25 août, 11:56, bjlockie <> wrote:
    >> 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}$/) {
    >


    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
    Josef Moellers, Aug 25, 2012
    #3
  4. bjlockie wrote:
    > 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
    --
    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
    John W. Krahn, Aug 27, 2012
    #4
  5. "John W. Krahn" <> writes:
    > bjlockie wrote:
    >> 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// ) {


    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').
    Rainer Weikusat, Aug 27, 2012
    #5
  6. bjlockie

    Willem Guest

    Rainer Weikusat wrote:
    ) "John W. Krahn" <> writes:
    )> 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
    Willem, Aug 27, 2012
    #6
  7. bjlockie

    John Black Guest

    In article <>, says...
    > some people love to use
    > because the very notion of doing anything in straight-forward way just
    > feels wrong to them.


    Funny (and true)...

    John Black
    John Black, Aug 27, 2012
    #7
  8. Ben Morrow wrote:
    >
    > Quoth :
    >> bjlockie wrote:
    >>> 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";
    >> }

    >
    > 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
    --
    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
    John W. Krahn, Aug 28, 2012
    #8
  9. "John W. Krahn" <> writes:
    > Ben Morrow wrote:
    >> Quoth :


    [...]

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


    [...]

    >> 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 ==.
    Rainer Weikusat, Aug 28, 2012
    #9
  10. On 2012-08-28 02:25, John W. Krahn <> wrote:
    > Ben Morrow wrote:
    >> Quoth :
    >>> bjlockie wrote:
    >>>> 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";
    >>>> }


    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".


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

    >>
    >> That's not the same.

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

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


    >> 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"


    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


    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
    Peter J. Holzer, Aug 30, 2012
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J
    Replies:
    1
    Views:
    281
    Kevin Spencer
    Aug 20, 2003
  2. wweric
    Replies:
    1
    Views:
    332
  3. Guest
    Replies:
    11
    Views:
    3,133
    Peter Seaman
    Dec 19, 2003
  4. Scott Allen
    Replies:
    0
    Views:
    2,084
    Scott Allen
    Apr 24, 2004
  5. Gabriella
    Replies:
    2
    Views:
    97
    Thomas 'PointedEars' Lahn
    Oct 12, 2007
Loading...

Share This Page