For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > can not filter out commentary lines with reg-exps









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author can not filter out commentary lines with reg-exps
Harald

2006-03-29, 7:57 am

Hi,

I need to parse an ASCII file which first rows are comments. Those rows
are marked by a '#' at their beginning.
I try to filter them out by

while( <$FH> =~ m{\#*} )
{}

But this does not work. The loop does never stop.
What goes wrong? I am really !

Regards and thanks a lot for your help in advance,
Harald


Bjorge Solli

2006-03-29, 7:57 am

On Wednesday 29 March 2006 14:22, Harald wrote:
> I need to parse an ASCII file which first rows are comments. Those rows
> are marked by a '#' at their beginning.
> I try to filter them out by
>
> while( <$FH> =3D~ m{\#*} )


while( <$FH> =3D~ m{^\#.*} )

> {}
>
> But this does not work. The loop does never stop.
> What goes wrong? I am really !


Your * refers to the #, saying you want to match all # occuring at least 0=
=20
times. My * refers to the . and so my regex says match a # at the beginning=
=20
of the line and the rest of that line (since * is greedy).

=2D-=20
Bj=F8rge Solli - Office:+47 55205847 cellph.:+47 91614343
Nansen Environmental and Remote Sensing Center - Bergen, Norway
http://www.nersc.no Reception: +47 55205800
Dept.: Mohn-Sverdrup Center for Global Ocean Studies=20
and Operational Oceanography
John W. Krahn

2006-03-29, 6:58 pm

Harald wrote:
> Hi,


Hello,

> I need to parse an ASCII file which first rows are comments. Those rows
> are marked by a '#' at their beginning.


You need to use '^' instead of '' to indicate the beginning of a line.

> I try to filter them out by
>
> while( <$FH> =~ m{\#*} )
> {}
>
> But this does not work. The loop does never stop.
> What goes wrong? I am really !


Your pattern says to match zero or more of the '#' character and every line in
your file has at least zero '#' characters. You want something like:

1 while <$FH> =~ /^#/;



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com