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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I replace a line of whitespace with -nothing- 1

Status
Not open for further replies.

LaylaNahar

Programmer
Oct 25, 2006
26
US
Hi there,

I have some files in which I want to eliminate lines with no text. currently I'm just eliminating those lines which have only the "\n" character on them like so:

if (($lines[$i] eq "\n") && ($lines[$i+1]) && ($lines[$i+2]))
{
chomp ($lines[$i]);
}

I'd like to replace most of the lines that are only whitespace. I feel pretty confident that I have detcted them, I am just not sure what perl syntax to use to eliminate lines that consist of more whitespace than just "\n". Here's what I've got. Can you recommend something for the body of this if statement?

if (($lines[$i] =~/\s/) && ($lines[$i+1]=~/\s/) && ($lines[$i+2] =~ /\s/))
{
# something to eliminate all characters
}


thank you very much!
LN
 
this is better to find lines with only spaces or only a newline:

Code:
/^\s*$/

but your code is looking for three consecutive elements of an array, why are you doing that?

You could do this:

Code:
if ($lines[$i] =~ /^\s*$/) {
    $lines[$i] = '';
}

the array element will still be present in the array but it will be empty.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
[snip]looking for three consecutive elements of an array, why are you doing that [snip]

I want to make sure that there are always at least two lines of whitespace between lines of non-whitespace.


Thank you for you answer - so simple! (I had tried using \0 - didn't work)
 
Kirsle,

for part of my task, I needed to eliminate all data. '' ,suggested by KevinACD worked very well. I think \0 should have worked, too. It just didn't work when I tried it, which may have been attributable to other errors in my code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top