Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: About a piece of code

by olus (Curate)
on Aug 05, 2009 at 10:47 UTC ( #786041=note: print w/ replies, xml ) Need Help??


in reply to About a piece of code

m/^\n/ is a regular expression that tries to match a newline (\n) at the beggining of the line (^). If there is such a match on the current value of $_, the code jumps back to the condition on the while loop, in this case reads one more line from the file.

So, this code is skipping blank lines on the file.

update as Marshall correctly points, the code skips lines that start with the \n disregarding whatever comes next.


Comment on Re: About a piece of code
Select or Download Code
Re^2: About a piece of code
by Marshall (Prior) on Aug 05, 2009 at 10:53 UTC
    This is skipping lines which only have a new line char at the start. m/^\s*$/ is a better way of saying a line with only white space characters because it allows any number of non-printing whitespace characters on the line. Whitespace is \n\f\s\f\r Update: oof..goof should be \n\f\s\t\r order doesn't matter, but there are 5 of these white space critters and I goofed! Ooops!

    m/^\n/ and next; ... }
    This is sort of a "cheater" way to execute the "next" if the match is true. Sometimes this is a good idea, sometimes not. I would probably code it like this:

    next if m/^\s*$/; #skip/re-prompt for blank lines #or if (m/^\s*$/) #very unlikely that I would write this { #but, it means the same as above next; } I would not write the below. This is obfuscated bad code. Because it obscures the "if" nature of the statement. m/^\s*$/ and next;
      Whitespace is \n\f\s\f\r Update: oof..goof should be \n\f\s\t\r order doesn't matter,

      \s is not a whitespace character.   In a regular expression it is a character class that includes the characters [ \t\n\r\f] and in a double quoted string it is the character s.

        \s is the single space ' ' character. In your set [ \t\n\r\f] that thing right before the \t is \s.

        Oooh I see now... in a character set [\s\t\f\r\n], \s means a single space.
        in a regex \s means all of the chars in this set: [\s\t\f\r\n]. Yep, confusing!!
        \s has a context dependent meaning. Such as it is.

Re^2: About a piece of code
by trewq (Initiate) on Aug 05, 2009 at 10:55 UTC
    olus Could you pls expand your explanation? Sorry, I am a beginner. and finally what exactly does "and next" do? Thanks.

      Take a look at Perl regular expressions. There you'll find the information you need to get you started on regexps.

      So, you have a regexp in that piece of code, and the and next; part is saying that, if the line that was read matches that condition, then you don't want to execute the rest of the code inside the while statement, you want to read a new line from input, meaning that the next code to be executed is while (<input>). That is to say, you go for another cycle of the while loop.

        olus you say: and next; part is saying that, if the line that was read matches that condition, then you don't want to execute the rest of the code inside the while statement, you want to read a new line from input, meaning that the next code to be executed is while (<input>).
        while (<input>) { m/^\n/ and next; m/^\n/ and next; ... }
        if the code was like above, wouldnt the second "m/^\n/ and next;" line be executed?

        Because you said "if the line that was read matches that condition, then you don't want to execute the rest of the code inside the while statement"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-04-19 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls