Ignore empty lines and ones that start w/...

Discussion in 'Perl Misc' started by Travis, Apr 16, 2008.

  1. Travis

    Travis Guest

    While reading a file, ignoring empty lines seems pretty
    straightforward, but how can I ignore a line that starts with
    something like <-- which I use for comments.

    ex:

    <-- Title -->
    <-- Date -->
    ....
    info to parse
    ....
    Travis, Apr 16, 2008
    #1
    1. Advertising

  2. Travis <> writes:

    > While reading a file, ignoring empty lines seems pretty
    > straightforward, but how can I ignore a line that starts with
    > something like <-- which I use for comments.


    The standard way of ignoring some lines while processing a file would
    be something like:

    while(<>) {
    next if <some condition>;

    # process line
    }

    So you'll just have to come up with a condition matching the lines you
    want to ignore. I would probably just use aa regular expression.

    //Makholm
    Peter Makholm, Apr 16, 2008
    #2
    1. Advertising

  3. Travis

    askingagain Guest

    On Apr 16, 10:55 am, Travis <> wrote:
    > While reading a file, ignoring empty lines seems pretty
    > straightforward, but how can I ignore a line that starts with
    > something like <-- which I use for comments.


    What about
    perl -lne 'print unless /^\s*($|<--.*-->)/'

    - HTH atul
    askingagain, Apr 16, 2008
    #3
  4. Travis

    Travis Guest

    On Apr 16, 1:48 am, askingagain <> wrote:
    > On Apr 16, 10:55 am, Travis <> wrote:
    >
    > > While reading a file, ignoring empty lines seems pretty
    > > straightforward, but how can I ignore a line that starts with
    > > something like <-- which I use for comments.

    >
    > What about
    > perl -lne 'print unless /^\s*($|<--.*-->)/'
    >
    > - HTH atul


    Actually I changed up my csv files to make it easier. Now i just want
    to next if the line is blank or starts w/ a # for a comment.

    I'm understand the next usage I'm just trying to understand the
    expressions like /^s*...
    Travis, Apr 16, 2008
    #4
  5. Travis <> wrote:
    > On Apr 16, 1:48 am, askingagain <> wrote:
    > > On Apr 16, 10:55 am, Travis <> wrote:
    > >
    > > > While reading a file, ignoring empty lines seems pretty
    > > > straightforward, but how can I ignore a line that starts with
    > > > something like <-- which I use for comments.

    > >
    > > What about
    > > perl -lne 'print unless /^\s*($|<--.*-->)/'
    > >
    > > - HTH atul


    > Actually I changed up my csv files to make it easier. Now i just want
    > to next if the line is blank or starts w/ a # for a comment.


    > I'm understand the next usage I'm just trying to understand the
    > expressions like /^s*...


    That's a regular expression, applied to to the line you just
    read for matching. If you don't know about regular expressions
    you better should get you a good book about Perl (or e.g.
    "Mastering Regular Expressions" by Friedl, which covers the
    use of regular expressions not only in Perl but also in several
    other languages and gives some theoretical background) - they're
    much too wide a topic to be covered by some news posting, but
    are one of the big strenghts of Perl and you will find them in
    nearly every Perl program you will look at.

    But to give you an idea here's what you would need if you want to
    skip blank lines or lines that have a '#' at the start (or after
    one or more white-space characters, i.e. spaces, tabs etc.):

    while ( <$f> ) {
    next if /^\s*($|#)/;
    do_something_with_the_line( $_ );
    }

    The stuff in between the slashes is the regular expression,
    applied to try to match the default argument '$_', which in
    this case is the line you just read in from the file.

    The '^' at the start says that the comparison is to start at
    the very beginning of the line. The '\s*' matches an unspe-
    cified number of white-space charecters (between 0 and as
    many as there are). The '$|#' means: either the end of the
    line ('$') or a '#' character, with the '|' being the or
    operator. Thus the whole line can be read as: next if the
    line just read starts with zero or more white-space charac-
    ters, followed by the end of the line or a '#' character.

    The paranthesis around the '$|#' are necessary because

    /^\s*$|#/

    would mean: match if the line either just comtains zero or more
    white space characters (i.e. it's a blank line) or if there's a
    '#' to be found anywhere within the line.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Apr 16, 2008
    #5
  6. (Jens Thoms Toerring) wrote in
    news:-berlin.de:

    > while ( <$f> ) {
    > next if /^\s*($|#)/;


    next if /^\s*(?:$|#)/;

    There is no need to capture anything.

    > do_something_with_the_line( $_ );


    I also prefer to use lexical variables if I need to pass anything
    further to another routine (just to avoid any possible action-at-a-
    distance effects).

    while ( my $line = <$f> ) {
    next if $line =~ /^\s+$/;
    next if $line =~ /^\s*(?:$|#)/;

    do_something( $line );
    }

    <great explanation snipped for brevity>

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Apr 16, 2008
    #6
  7. In <<>>
    schrieb Travis...

    > I'm understand the next usage I'm just trying to understand the
    > expressions like /^s*...


    For quick/in-depth reference on regular expressions have a look at these
    documents provided with your Perl distribution:

    perldoc perlrequick
    perldoc perlretut
    perldoc perlreref

    where 'perldoc' is the command you type at the command prompt,
    'perlre...' is the document to be displayed.


    hth, Hartmut

    --
    ------------------------------------------------
    Hartmut Camphausen h.camp[bei]textix[punkt]de
    Hartmut Camphausen, Apr 17, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Wright
    Replies:
    0
    Views:
    497
    Joe Wright
    Jul 27, 2003
  2. Justme
    Replies:
    9
    Views:
    601
    clayne
    Oct 1, 2006
  3. Murali
    Replies:
    2
    Views:
    542
    Jerry Coffin
    Mar 9, 2006
  4. Rob Meade
    Replies:
    6
    Views:
    254
    Rob Meade
    Mar 1, 2004
  5. Replies:
    2
    Views:
    108
    Damian James
    May 4, 2005
Loading...

Share This Page