Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

how to skip certain lines and check for blank ones?

by sanjay nayak (Sexton)
on Oct 31, 2006 at 06:42 UTC ( [id://581427]=perlquestion: print w/replies, xml ) Need Help??

sanjay nayak has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on how to skip certain lines and check for blank ones?

Replies are listed 'Best First'.
Re: Suggest me a suitable title
by davorg (Chancellor) on Oct 31, 2006 at 08:46 UTC

    This is at least the fifth or sixth node that you have posted with the title "Suggest me some suitable code" or something similar. Each time you have been told that this is a bad title and each time the site janitors have changed the title to something more suitable.

    Please take the time to name your nodes sensibly. The node title should be a brief summary of the problem. For example a good title for this problem might be something like "Skipping Comment Lines". Many people use the title of a post to decide whether to bother reading it. By using bad titles as you currently do, you might be unnecessarily limiting the number of people who read your posts.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: how to skip certain lines and check for blank ones?
by ptum (Priest) on Oct 31, 2006 at 15:12 UTC
Re: how to skip certain lines and check for blank ones?
by davido (Cardinal) on Oct 31, 2006 at 07:42 UTC

    I might do something like this:

    use strict; use warnings; open my $infile, '<', 'sss.txt' or die $!; while( my $line = <$infile> ) { $line =~ /^$/ and die "Blank line detected at $.\n"; $line =~ /^#/ and next; print "$.: $line"; } close $infile;

    The first regexp matches (thus triggering the die) if the line is empty, or empty with a newline character at the end.

    The second regexp matches (thus skipping to the next line) if a '#' is found as the first character of the line.


    Dave

      $line =~ /^$/ and die "Blank line detected at $.\n";

      Because people have differing opinions about what a blank line is, I'd probably replace that line with:

      $line =~ /\S/ or die "Blank line detected at $.\n";
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: how to skip certain lines and check for blank ones?
by jkva (Chaplain) on Oct 31, 2006 at 06:54 UTC

    To help you along the way : I'd iterate over the lines and use a Regular Expression at each line to determine the next action to take.

    Good luck :)

Re: how to skip certain lines and check for blank ones?
by mickeyn (Priest) on Oct 31, 2006 at 07:30 UTC
    first of all, read perlre.

    a code that may suit your purpose might be something like:

    my $INFILE_file_name = 'your_file'; open my $INFILE, '<', $INFILE_file_name or die "$0 : failed to open input file $INFILE_file_name : $ +!\n"; while (<$INFILE>){ next if ( m{ \A \s* # }x); ... your code here ... } close $INFILE or warn "$0 : failed to close input file $INFILE_file_name : $ +!\n";

    HTH,
    Mickey

Re: how to skip certain lines and check for blank ones?
by DrHyde (Prior) on Nov 01, 2006 at 10:53 UTC
    Downvoted (and considered for retitling) because you were too lazy to think of a good title.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found