Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

•Re: Re: •Re: Checking for single digit

by merlyn (Sage)
on May 06, 2003 at 02:28 UTC ( [id://255789]=note: print w/replies, xml ) Need Help??


in reply to Re: •Re: Checking for single digit
in thread Checking for single digit

and that chomping is done,
Chomping wasn't done here. And none of the other answers (that I saw) mentioned the restrictions on their match.

Hence, I must point it out.

Cargo cult is when you use something you don't fully understand, and then pass it on, without understanding or describing its limitations. This was a perfect example of at least three people cargo-culting the answer.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re: Re: •Re: Checking for single digit

Replies are listed 'Best First'.
Re: Checking for single digit
by Abigail-II (Bishop) on May 06, 2003 at 07:46 UTC
    Actually, considering chomping was not done, the original answer, /^\d$/ would be correct, unlike your answer, which would never match, as there would always be the trailing newline.

    The fact that $ can match just before a trailing newline makes cases like this "do what I mean".

    Abigail

Re: •Re:^2 •Re: Checking for single digit
by Enlil (Parson) on May 06, 2003 at 04:28 UTC
    rather than continuing to cargo-cult code. I have a question refering to your answer (i.e. /\A\d\z/), that being, when there are no modifiers on the regular expression what is the difference between \A and ^? (i have read perlre and gone through MRE, but want to make sure I am not missing any subtlies).

    While I agree that your answer is better than mine in some respects, the OP did not have a chomp statement, and I assumed perhaps incorrectly that someone would type in a number and thus the newline character would go along with whatever number (or other input) the user typed in and not being chomped out (or removed by any other method), then nothing would ever match with \z (so I am at fault for making so many assumptions and not clarifying more fully). I think at least a couple of the answers (including mine) do mention the fact that $ matches the end of the line or right before the newline character at the end of the line.

    But as I mentioned before, I do agree with you, if all you want in a string is to insure that the string is only one digit without anything else in the string (and your using a regex for the checking), then /\A\d\z/ is the correct way to go about it. :)

    -enlil

      I checked because I can never remember the difference myself. From the camel, 3rd ed., page 179:

      The \A assertion matches only at the beginning of the string, no matter what. However, the ^ assertion is the traditional beginning-of-line assertion as well as a beginning-of-string assertion. Therefore, if the pattern uses the /m modifier and the string has embedded newlines, ^ also matches anywhere inside the string immediately following a newline character.

      So, without the /m modifier, ^ and \A are identical.

      For completeness (same page):

      The \z metasymbol matches at the end of the string, no matter what's inside. \Z matches right before the newline at the end of the string if there is a newline, or at the end if there isn't. The $ metacharacter usually means the same as \Z. However, if the /m modifier was specified and the string has embedded newlines, then $ can also match anywhere inside the string right in front of a newline.
      90% of every Perl application is already written.
      dragonchild
Re: •Re: Re: •Re: Checking for single digit
by crouchingpenguin (Priest) on May 06, 2003 at 03:45 UTC

    Wouldn't this be more versatile (chomp or no chomp)?

    while(defined(my $line = <STDIN>)){ print $1,"\n" if( $line =~ m/\W?(\d)\W?/ ); }

    Update As Enlil pointed out, {1} is implied by \d... I guess I more or less meant it to color the fact it was only one digit hungry.

    YA update Ok, i will try to actually read before posting next time. As Cody Pendant pointed out, the intent seems to be only these characters, not CONTAINS these characters.


    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://255789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-24 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found