Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Removing blank lines from array

by Jemts (Monk)
on Feb 28, 2002 at 16:05 UTC ( #148260=perlquestion: print w/ replies, xml ) Need Help??
Jemts has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a program for my web site that will read a txt file with info in it and print part of it to the screen(like the first 43 lines) and then have a link to the next 43 lines. But when i read from the txt file and store it in an array it stores blank lines as elements of the array( which I don't want) but when I try to remove them using grep it doesnt shrink the array. Here is my code:
$article=$formdata{'article'}; $page=$formdata{'page'}; $maxlength=43; $start=($page-1)*$maxlength; $finish=$page*$maxlength; $next=$page+1; $previous=$page-1; &main; sub main{ print "Content-type: text/html\n\n"; $file="$basedir$article\.txt"; open(READ, "$file"); @info=<READ>; close(READ); @info=grep(!/^ +$/, @info); $length=@info; for($i=$start;$i<$finish;$i++){ print"$info[$i]\n"; } if (($length>$finish)&&($page>1)){ print"<table border=0><tr><td align=left>&nbsp&nbsp<font color=yellow> +<- </font>&nbsp<a href=\"http://www.mysite.net/cgi-bin/article.cgi?articl +e= $article&amp;page=$previous\">Continue to the previous page</a></td><t +d width=200></td><td align=right><a href=\"http://www.mysite.net/cgi- bin/article.cgi?article=$article&amp;page=$next\">Continue to the next + page</a>&nbsp<font color=yellow>--></font>&nbsp&nbsp</td></tr></table> +\n"; }elsif(($length>$finish)&&($page==1)){ print"<table border=0><tr><td align=right><a href=\"http://www.mysite. +net/cgi- bin/article.cgi?article=$article&amp;page=$next\">Continue to the next + page</a>&nbsp<font color=yellow>--></font>&nbsp&nbsp</td></tr></table> +\n"; } }

Instead of print our the 43 it prints out a few lines then a whole lot of blank spaces. Can anyone help?

Jemts

"If you're traveling in a time machine, and you're eating corn on the cob, I don't think it's going to affect things one way or the other. But here's the point I'm trying to make: Corn on the cob is good, isn't it."

Comment on Removing blank lines from array
Download Code
Re: Removing blank lines from array
by dragonchild (Archbishop) on Feb 28, 2002 at 16:12 UTC
    @info=<READ>; @info=grep(!/^ +$/, @info);
    should be written as
    @info = grep {!/^\s+$/} map { chomp; $_ } <READ>;
    You're not catching the newline in your regex. *shrugs*

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Removing blank lines from array
by strat (Canon) on Feb 28, 2002 at 16:19 UTC
    Or even better as:
    my @info = grep { !/^\s*$/ } <READ>; chomp(@info);
    Then empty lines (with no whitespace) will be skipped, too

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

        Shouldn't that be:
        my @info; chomp(@info = grep /\S/, <READ>);
        so that @info is accessible later?

        /prakash

Re: Removing blank lines from array
by jjohn (Beadle) on Feb 28, 2002 at 17:35 UTC

    Unsurprisingly, Randal's solution is on the nose for your question, but I thought I might talk about a related issue. Why are you reading in the whole file at once when you really only want the first 43 lines? This is very wasteful for large files. You might consider a more verbose while loop like the following:

    print $html_preamble; # whatever's prolog to the real content open READ, $file or die "Oops! can't read $file: $!"; my $cnt = 0; while(my $line = <READ>){ last if $cnt >= 43; # do we count rejected lines? no. next unless $line =~ /\w/; # lines with visible content print $line; $cnt++; } close READ; print $html_footer;

    I understand your code is doing some pagination, so the above code is only a sketch of the solution, but I think this solution is a bit easier to maintain and handles large files better.

    cheers!

    Edit ar0n -- switched <pre> tags to <code>

      As small improvement, you can use $. instead of $cnt, as is documented in perlvar.

        Actually, his specific point was that one probably didn't want to count the number of lines read from the file, but instead the number of lines printed. Because of the presence of the next statement, they're not the same thing. Thus the use of the $cnt variable to keep track of the number of lines printed.

        perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      I am intrigued greatly by jjohns solution to my problem but i dont't understand when he set up his while loop using
      while(my $line = )
      the loop has no condition, won't that give you an error

      Jemts

      "If you're traveling in a time machine, and you're eating corn on the cob, I don't think it's going to affect things one way or the other. But here's the point I'm trying to make: Corn on the cob is good, isn't it."
        I am intrigued greatly by jjohns solution to my problem but i dont't understand when he set up his while loop using
          while(my $line = )
        
        the loop has no condition, won't that give you an error

        I guess it was a typo. The proper line would read:

          while(my $line = <READ>)
        
        Maybe jjohn forgot to type the angle brackets as ampersand HTML entities (&lt; and &gt;), he just typed the single characters, and your HTML renderer has considered it was a tag, and an invalide one, and discarded it.
        He probably used <pre> instead of <code>...
        --
        Snazzy tagline here

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://148260]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (16)
As of 2014-07-14 14:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (263 votes), past polls