Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Removing blank lines from array

by jjohn (Beadle)
on Feb 28, 2002 at 17:35 UTC ( [id://148301]=note: print w/replies, xml ) Need Help??


in reply to Removing blank lines from array

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>

Replies are listed 'Best First'.
Re: Re: Removing blank lines from array
by fundflow (Chaplain) on Feb 28, 2002 at 18:54 UTC
    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'

Re: Re: Removing blank lines from array
by Jemts (Monk) on Feb 28, 2002 at 19:18 UTC
    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
        All true! I forgot about <code> and those durn HTML entities. In a few more years, I get the hang of this "web" thing I've heard so much about. :-D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://148301]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found