[VPM] delete blank lines in src files using regex

Darren Duncan darren at DarrenDuncan.net
Sat May 20 21:43:24 PDT 2006


At 9:14 PM -0700 5/20/06, Jer A wrote:
>hello there,
>I am try to tidy up my csharp src files before i put them through a custom
>parser (a project i am working on.
>using regular expressions, i am trying to get rid of all blank lines,
>whether they are padded with tabs and/or space and/or newlines.
>basically i would like the my csharp code to use every line in the src file.
>Indentation does *Not* matter to me.
>included is a bit of my perl code, where i attempt, but fail to remove all
>blank lines.
>Your help is appreciated.
>Thank you,
>-Jer A.

So in other words, you want to keep all source lines that contain 
non-whitespace characters.

This should be sufficient:

   for (@input_lines) {
     if ($_ =~ m/\S/) {
       push( @output_lines, $_);
     }
   }

Or more tersely (and what I prefer to say myself):

   @output_lines = grep { $_ =~ m/\S/ } @input_lines;

The grep form looks better for simpler cases if you're keeping the 
whole file in memory at once.

Only use the for-loop then if you're keeping just one file line at a 
time in memory, and spitting the rest out to another file, to save on 
RAM.

-- Darren Duncan

P.S.  Please don't cc my personal address with these questions.  Just 
address them to the list and I'll see them there.  (I replied to the 
list copy as it is.)


More information about the Victoria-pm mailing list