Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

--

flounder


In reply to Re: checking the end of line by flounder99
in thread checking the end of line by Sara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found