Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Removing white-lines...

by saintly (Scribe)
on Apr 06, 2007 at 02:35 UTC ( [id://608590]=note: print w/replies, xml ) Need Help??


in reply to Removing white-lines...

$file_data =~ s/\n\n+/\n/gs;
Should work. On an unrelated note:
# Suggest using scalar filehandles and checking to # see if open succeeded... open( my $my_filehandle, $file ) || next; # possibly: || die "Can't open file: $!"; local $/ = undef; # tell perl not to stop reading at newline my $file_data = <$my_filehandle>; close $my_filehandle; # process $file_data # etc...
The 'open' command is very prone to failure on UNIX systems for lots of reasons (you don't have access to the file, it's not readable, the file's really a directory, etc...), so getting in the habit of checking it is a plus.

If you use scalar filehandles, you can use them with 'my' to keep them local to your block.

local $/ = undef; tells perl that newlines shouldn't be considered the 'end of input'. If you want to look at the whole file instead of individual lines, you can turn it off inside the block and the next read on the filehandle will give you the whole thing.

The 's' option after the substitution regex tells perl to not treat newlines as the end of the string in a regular expression. Then you can treat them as normal characters and remove them when there are a few in a row.

If lines have spaces on them (and you want to remove those 'empty' lines too), then:
$file_data =~ s/\n[\s\n]+/\n/gs;
should work.

You can do this task from the command line:
$ perl -ni -e 'print if /\S/' *.shtml
Although that's technically a line-by-line approach.
Update: Fixed typo ($/ not $\);
Update 2:Arr! Fixed problem #2 (parens around open, cause I don't like to use 'or' instead of '||');

Replies are listed 'Best First'.
Re^2: Removing white-lines...
by ikegami (Patriarch) on Apr 07, 2007 at 06:41 UTC

    open my $my_filehandle, $file || next;
    means
    open my $my_filehandle, ($file || next);
    which is quite wrong.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2025-08-11 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.