Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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]
shmem compiles himself into his template

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-06-25 20:55 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (570 votes). Check out past polls.