Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello perlnoobster,

"I just want to omit them and tried chomp but it didn't work ..."
This is because chomp is used to remove trailing strings (most often, newlines) from records. So instead of removing the blank line, it removes the trailing newline at the end of the blank line resulting in an empty string.

As stated by Anonymous Monk above, you could use a regex to rewrite

push @Results, $_ unless ($_ eq "\n")
like
push @Results, $_ unless /^\s*$/;
or even (depending on how much you trust your data),
push @Results, $_ unless /^$/;
Have a look at perlretut if you need help understanding basic regexs.

On a side note, your code will be much easier to read and debug if you adopt a style that uses proper indentation. The indentation will help you to better visualize conditional blocks and control structures, as well as help you to gain a better understanding of variable scope.

Here's a sample rewrite fixing the indentation with a few small modifications.

#!/usr/bin/perl #Always use strict; use warnings; #Use lexical variables instead of bareword filehandles open my $in, "<", "input_file" or die "Couldn't read input_file: $!\n"; my @results; while (<$in>) { chomp; push @results, $_ unless /^\s*$/; } #Close filehandle's when you're done with them. close $in; open my $out2, ">>", "output_file" or die "Couldn't open output_file: $!\n"; for (@results) { print $out2 "$_\n" if s/.*\///; } close $out2;


In reply to Re: Removing blank/empty lines by marquezc329
in thread Removing blank/empty lines by perlnoobster

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-04-18 11:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found