Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

checking the end of line

by Sara (Acolyte)
on Jul 19, 2002 at 22:31 UTC ( #183482=perlquestion: print w/ replies, xml ) Need Help??
Sara has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks ,, I have this variable
$path = hiec/by/mlor/kkss23/dndd@@mani/css.cpp/0
the number 0 at the end could change ,, I need a way of checking that number weather it is a 0 or 1 or etc. I am doing somthing like this
$path =~ (m/\d+$\/); if ($path == 0) {} else {}
but dont seem to work thanks for help ..

Comment on checking the end of line
Select or Download Code
Replies are listed 'Best First'.
Re: checking the end of line
by Fastolfe (Vicar) on Jul 20, 2002 at 00:13 UTC
    Hi Sara, I couldn't help but notice that you've posted a lot of questions here at PerlMonks that are all very similar to each other. In addition I've seen posts from others that are also fairly similar to your own.

    If you don't mind me asking, what exactly do you hope to gain out of PerlMonks here? A lot of people have been providing you with answers in the form of raw code, but it doesn't look like you've learned a whole lot through your petitioning.

    In addition, we tend to frown on requesting code to solve homework problems. While we've no real evidence that you're doing this as part of your homework, the fact that you have a shadow here posting very similar questions makes me wonder.

    It might help us if you took a step back and tried to think of what you're wanting to learn here and ask for help learning these concepts instead of asking for help solving very specific (and seemingly contrived) text processing problems over and over again.

      My guess (fifty-fifty) - this is probably a group of students who get help from perlmonks and they use single login?
      This will explain asking similar questions over and over again.

      Courage, the Cowardly Dog.

Re: checking the end of line
by dimmesdale (Friar) on Jul 19, 2002 at 22:38 UTC
    if ($path == 0)

    Remember, you said $path was: 'hiec/by/mlor/kkss23/dndd@@mani/css.cpp/0'.

    Try something like this:

    if($path =~ /(\d+)$/ == 0) {} else {}

    Note that what you used never captured the resulting match (that is, it was used in void context and had not side effects). Using the parentheses like I do, it sets the $1 variable (see perlre) -- note, too, that it also returns the value which I check, so you can say something like  $my_value = ...regex_here...

      You mean
      if( ($path =~ /(\d+)$/)[0] eq 0) {} else {}
        Do I?

        For a string 'stuff_here_then_12' your version captures the 1. It depends if that's what the user wants. Of course there's probably a better way to do this /(\d)\d*$/ comes to mind (and /(\d)+$/ if the user wants the last digit, or even better /(\d)$/ .. updated .. well, if just the last digit is desired and it's always known to be a digit substr could be used more effeciently -- of course, you have to be sure of your data)

        I was under the impressino that he wanted the entire 12 returned.

(joshua)Re: checking the end of line
by joshua (Pilgrim) on Jul 19, 2002 at 22:36 UTC
    Something like this...
    #!/usr/bin/perl -w use strict; my $path = 'hiec/by/mlor/kkss23/dndd@@mani/css.cpp/0'; $path = chop($path); if ($path == 0) {} else {}
Re: checking the end of line
by flounder99 (Friar) on Jul 20, 2002 at 13:44 UTC
    Rather than just blindly giving you an answer or accuse you of cheating on homework, I will try to explain what your code is doing. That way you can fix it yourself and maybe learn something in the process.
    $path = hiec/by/mlor/kkss23/dndd@@mani/css.cpp/0
    You are trying to assign a string to a variable. This won't work without quotes and a trailing semicolon. It is also a very good habit to add use strict; at the beginning of your program an use my on all of your variables. This is not required but it will save you a lot of headaches in the long run.
    $path =~ (m/\d+$\/);
    I think you have a typo. What you are doing is looking for digits followed by the value of the $\ special variable ($\ is the $OUTPUT_RECORD_SEPARATOR). I don't think this is what you want. I will take the liberty of changing the typo to what I think you want.
    $path =~ (m/\d+$/);
    Hmmmm. This line does essentially nothing. You are checking if there are numbers at the end of the string stored in $path. But you are not using the return value to see if the match succeeded and you are not capturing anything in the regexp. A simple match does not alter the string either. If you want to capture the digits you will have to add parentheses arround the \d+. If you want to change $path use a search/replace ( s/// ) operator. But be carefull. If you do something like a $path =~ s/.*?(\d+)$/$1/; and $path doe not end in digits $path will not be changed. Since you are doing a numeric compare below, the number that will be used will be the digits at the start of $path!

    Here is the way I would do it.

    use strict; my $path = 'hiec/by/mlor/kkss23/dndd@@mani/css.cpp/0'; if ($path =~ m/(\d+)$/ and $1 == 0) { # ends in zero } else{ # not zero or no digits }
    What I do is check if $path ends in digits. If it does not, the compare on the right side of the and never is executed and the if statement fails so control goes to the else block. If $path ends in digits then the digits are captured into $1 due to the parentheses. $1 is compared to zero. If it is zero the first block is executed. If not then the else block is executed.

    OK, so I gave you code. I hope you understand it. If you do and are getting help on homework then all the better. Understanding Perl is what the homework is all about.



Re: checking the end of line
by Basilides (Friar) on Jul 20, 2002 at 14:59 UTC
    flounder's explanation was really thorough, but just to build on it a bit, it looks like you want to first make sure that your string ends in a digit (good practice), then, as long it it does, do different things depending on which number it is. That would look something like this:
    if ($path =~ m/(\d+)$/) { my $end_num = $1; if ($end_num == 0) { etc. } and so on for other numbers } else { print "Oh dear, my path doesn't end with a number\n"; }
    One other thing to note is that this'll check for one *digit* at the end of your string, so if your end number might go into double figures remember to stick a '+' sign into your regex:
    Hope that helps

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://183482]
Approved by Courage
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2016-05-04 02:39 GMT
Find Nodes?
    Voting Booth?