Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing multiple blank lines from file array 2

Status
Not open for further replies.

Xaqte

IS-IT--Management
Oct 4, 2002
971
US
I know how to loop through an array and check for blank lines, but I cannot figure this one out.

Typically, this is what I use to check for blank lines:
Code:
foreach my $ln(@html) {
    	unless($ln eq "\n" ){
	print $ln;
	}
}

or:

Code:
foreach my $ln(@html) {
    	unless($ln =~ /^\s$/ ){
	print $ln;
	}
}

My question is:
How can I remove blank lines ONLY if a blank line is before it AND after it?

Thanks in advance for any thoughts/suggestions!


X
 
yon can just create another array like so:

my @html_new

foreach my $ln(@html)
{
unless($ln eq "\n" )
{
push @html_new, $ln;
}
}

now @html_new will have all the lines except blank lines
 
mactonio,
Thanks for the quick response! However, I don't think you understood my question... sorry if I was vague.

Looking at your suggestion, this will remove all blank lines from the original array (@html).

What I want to know is:
How can I remove blank lines only if a blank line is before it and after it?

For example:
Turn this:
Code:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse dui mauris, tincidunt nec, tincidunt id, lobortis sed, nisi.

Duis molestie, metus ac pellentesque aliquet, leo purus facilisis nulla, sit amet pharetra lectus erat ac est. Duis fermentum eros ac nunc. Curabitur quis ipsum a augue tristique dapibus.



Duis molestie, metus ac pellentesque aliquet, leo purus facilisis nulla, sit amet pharetra lectus erat ac est. Duis fermentum eros ac nunc. Curabitur quis ipsum a augue tristique dapibus.







Duis molestie, metus ac pellentesque aliquet, leo purus facilisis nulla, sit amet pharetra lectus erat ac est. Duis fermentum eros ac nunc. Curabitur quis ipsum a augue tristique dapibus.

Into this:
Code:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse dui mauris, tincidunt nec, tincidunt id, lobortis sed, nisi.

Duis molestie, metus ac pellentesque aliquet, leo purus facilisis nulla, sit amet pharetra lectus erat ac est. Duis fermentum eros ac nunc. Curabitur quis ipsum a augue tristique dapibus.


Duis molestie, metus ac pellentesque aliquet, leo purus facilisis nulla, sit amet pharetra lectus erat ac est. Duis fermentum eros ac nunc. Curabitur quis ipsum a augue tristique dapibus.


Duis molestie, metus ac pellentesque aliquet, leo purus facilisis nulla, sit amet pharetra lectus erat ac est. Duis fermentum eros ac nunc. Curabitur quis ipsum a augue tristique dapibus.

Thanks again!

X
 
My question is:
How can I remove blank lines ONLY if a blank line is before it AND after it?

can you explain that better or provide a sample before and after array.
 
Try this..


my @new=();

push(@new,$html[0]);

for (my $loop=1;$loop<((scalar @html)-1); $loop++)
{
unless(($html[$loop-1] eq "\n") && ($html[$loop] eq "\n") && ( $html[$loop+1] eq "\n"))
{
push(@new,$html[$loop]);
}


}
 
maybe read the data into a scalar and use substitution:

Code:
open(FH,'myfile.txt') or die "$!";
my $data = do { local $/; <FH>};
close FH;
$data =~ s/\n{3,}/\n\n/g;
print $data;
 
vjcyrano,
Very nice! However, I had to change this line:
Code:
unless(($html[$loop-1] eq "\n") && ($html[$loop] eq "\n") && ( $html[$loop+1] eq "\n"))
To this:
Code:
unless(($html[$loop-1] =~ /^\s$/ ) && ($html[$loop] =~ /^\s$/ ) && ( $html[$loop+1] =~ /^\s$/ ))

It works great!

One final question, I would really like to understand this one line. If anyone could explain it, I would appreciate it:
Code:
for (my $loop=1;$loop<((scalar @html)-1); $loop++)

Thanks again!

X
 
it's a long winded way to loop through an array using the index number of each element of the array, it really could be written more perlish:

Code:
for(1 .. $#html) {
   unless(($html[$_-1] =~ /^\s$/ ) && ($html[$_] =~ /^\s$/ ) && ( $html[$_+1] =~ /^\s$/ )) {
      push($data,$html[$_]);
   }
}
 
KevinADC,
Thats pretty nice!

Here is what the benchmark shows:
Code:
Benchmark: running kevinadc, vjcyrano for at least 3 CPU seconds...
  kevinadc:  2 wallclock secs ( 3.06 usr +  0.00 sys =  3.06 CPU) @ 621028.40/s
(n=1902210)
  vjcyrano:  4 wallclock secs ( 3.00 usr +  0.00 sys =  3.00 CPU) @ 601542.33/s
(n=1804627)

Thumbs up to both of you!

Thanks again!

X
 
You may want to subtract one from $#html, like vjcyrano did in his code ( $loop<((scalar @html)-1 ), to avoid any out of bounds problems:

for(1 .. $#html-1) {
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top