Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: File read and strip

by Jasper (Chaplain)
on Nov 15, 2006 at 10:44 UTC ( #584137=note: print w/ replies, xml ) Need Help??

in reply to File read and strip

I think the main problem with the OPs code was that he was doing the exact opposite of what he wanted, but no one really seems to have pointed that out explicitly, it seems to me.

for(@array){ ($i)= grep { m/^#/ } $_; push @new, $i; }
dumps all the comments in @new, not the lines without comments. The corrected solution has been shown, of course.

Also, the OP is reading the file line by line in one loop, then extracting from that file into an array in another loop, then looping over that array to print to the outfile. This can all be done in one loop (like the one liner)
while (<INFILE>) { next if /^#/; print OUTFILE $_; }
I'd also add in a whitespace element to the regex, because I rarely start comments at the beginning of the line - I usually indent them with the rest of the code, and I doubt I'm the only one. so /^\s+#/


perl -ne '/^#/ or print' perl -pe '$_ = "" if /^#/' perl -pe '$_ x= !/^#/'

Comment on Re: File read and strip
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://584137]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2015-10-09 14:06 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (242 votes), past polls