Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Removing blank/empty lines

by perlnoobster (Sexton)
on Dec 11, 2012 at 13:34 UTC ( #1008301=perlquestion: print w/replies, xml ) Need Help??
perlnoobster has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks,

I was wondering if someone could help me, I have some coding that generates a text file, however there are blank rows that are being generated too, I just want to omit them and tried chomp but it didn't work so I tried using push @Results,$_ unless ($_ eq "\n"); and that didn't work, please can someone help me?

open (IN, "$output") or die "I couldn't get at $output"; my @Top_URLs = <IN>; foreach my $URL (@Top_URLs) { my $final_page = get "$URL";#grab the link urls get $url my @Results =(); push @Results,$_ unless ($_ eq "\n"); for my $Results (@Results) { if ($Results =~ s/.*\///) { print OUT2 "$Results\n"; } } }

Replies are listed 'Best First'.
Re: Removing blank/empty lines
by toolic (Bishop) on Dec 11, 2012 at 14:03 UTC
      hi, thank you for replying, it does not show what I expect it to. it indicates a syntax error near ") print"? Please keep in mind that I am very new to perl and learning bits and pieces :)

        Make sure you put it inside the loop. The "{ }" brackets contain everything that's inside of your loop.

        Syntax error it says. There's a close bracket before your print it says.

        Look at the code. Is that the close bracket on your loop list? Did you put the new print inside the curly {}'s of your loop?

Re: Removing blank/empty lines
by marquezc329 (Scribe) on Dec 11, 2012 at 17:37 UTC

    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")
    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;

      ....or you could use grep (either within a Unix shell or the Perl grep function) to do the job for you. :-)

      A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Removing blank/empty lines
by Anonymous Monk on Dec 11, 2012 at 14:28 UTC
    Use a regex like /$\s*^/ to anchor the pattern at both ends and search for zero-or-more occurrences of a space character between.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008301]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2018-05-26 08:29 GMT
Find Nodes?
    Voting Booth?