Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Ignore empty lines and ones that start w/...

Reply
Thread Tools

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

 
 
Travis
Guest
Posts: n/a
 
      04-16-2008
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
....

 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      04-16-2008
Travis <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
askingagain
Guest
Posts: n/a
 
      04-16-2008
On Apr 16, 10:55*am, Travis <(E-Mail Removed)> 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
 
Reply With Quote
 
Travis
Guest
Posts: n/a
 
      04-16-2008
On Apr 16, 1:48 am, askingagain <(E-Mail Removed)> wrote:
> On Apr 16, 10:55 am, Travis <(E-Mail Removed)> 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*...
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      04-16-2008
Travis <(E-Mail Removed)> wrote:
> On Apr 16, 1:48 am, askingagain <(E-Mail Removed)> wrote:
> > On Apr 16, 10:55 am, Travis <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-16-2008
(E-Mail Removed) (Jens Thoms Toerring) wrote in
news:(E-Mail Removed)-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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
Hartmut Camphausen
Guest
Posts: n/a
 
      04-17-2008
In <<(E-Mail Removed)>>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
ow can i use fgets to read and ignore the first two lines of a file and output into another file Justme C Programming 9 10-01-2006 02:26 PM
Hard drives--Are big ones more delicate than smaller ones? PowerPost2000 Computer Support 2 03-01-2005 10:30 PM
Ignore + TEST + Ignore SpooderStank Computer Support 2 04-08-2004 11:26 AM
Searching for Exact Phrase - should I ignore the ignore words? Rob Meade ASP General 6 03-01-2004 11:28 AM



Advertisments