[Za-pm] Regular expression help needed
Nick Cleaton
nick at cleaton.net
Mon Jun 23 08:26:49 CDT 2003
On Mon, Jun 23, 2003 at 02:26:12PM +0200, Pritesh Jewan wrote:
>
> I have a few regular expressions from a script that I am trying to update
> and needed some assistance with what the expression mean:
>
> ===============================
> while(<GWTABLE>){
> next if (/^#/);
'next' means jump to the next go around the loop.
'if (/^#/)' means 'if the current line starts with a #'.
So that line jumps to the next go around the while loop (i.e. goes
on to the next line from GWTABLE) if the current line starts with
a '#'.
> next if (/^$/);
Similar, but '$' means the end of the string, so this one does a
'next' if the line is empty.
You don't need the brackets on those, and I find them more readable
without brackets, so I would write those as:
next if /^#/;
next if /^$/;
> $match = 1;
>
> I'm trying to understand the second and third line.
> ===============================
> The script has this line in a few places. Can't seem to find any doco that
> would explain what it means:
>
> s(\n)();
That deletes the newline from the end of the line. I prefer to write
that as:
chomp;
since I find that more readable.
> s/\n$//;
Ditto. I would also rewrite that as:
chomp;
> ===============================
> (!/^$/)||next;
> (!/^#/)||next;
Those are confusing double negatives. They jump to the next go
around the loop if the pattern matches. I would rewrite those
as:
next if /^$/;
next if /^#/;
--
Nick
More information about the Za-pm
mailing list