Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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= !/^#/'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://584137]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-05-24 21:52 GMT
Find Nodes?
    Voting Booth?