Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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+#/

Jasper

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


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-07-23 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (152 votes), past polls