in reply to Support for hash comments on a line

Not real sure why everyone posted regex solutions here, use substr and index. Much faster.
while (<DATA>) { if ( ( my $p = index( $_, "#" ) ) > -1 ) { substr( $_, $p, -1, "" +) } next if /^\s*$/; print; } __DATA__ #comment this is test#comment #comment this is test #comment this is test #comment this is test #comment
A regex in any form (split, s/// or m//) is overkill for this task. (Assuming of course that # can't appear in the real data)

Yves / DeMerphq
--
Have you registered your Name Space?

Replies are listed 'Best First'.
Re: Re: Support for hash comments on a line (Why use a regex at all?)
by Fastolfe (Vicar) on Nov 04, 2001 at 03:30 UTC
    Unless you're dealing with a large number of lines here, the performance penalty of going with a regex is, in my opinion, inconsequential compared with the added readability of code that uses it. No one skimming the code above is going to have the slightest idea what it does without studying it.

    Though don't get me wrong, if your requirements are such that you're going to be doing this sort of processing on a lot of data, and performance is a factor, this is one of many optimizations that can be made to squeeze speed out of the algorithm.