in reply to reading 100 line at a one time

Update 2 : It has come to my attention that the code down here is very wrong. Ignore it!
Update : This should do the trick.
It opens "C:/test.txt" , reads the lines into an array, loops through the array, printing every 100th slice, if that array slice is initialized.
my @lines = (); open THISFILE, "c:/test.txt"; while(<THISFILE>) {push(@lines,$_);} close THISFILE; for(my $i = 0; $i <= 1000; $i += 100) {print $lines[$i] || "";}

1

Replies are listed 'Best First'.
Re^2: reading 100 line at a one time
by halley (Prior) on Mar 01, 2005 at 15:58 UTC
    If the file contains 10_000 lines, you are loading them all. What about 10_000_000 lines? Can your system handle that?

    The original poster wanted to read 100 at a time. Though I doubt that they'll notice any real advantage, in terms of I/O, because Perl already does buffering behind the scenes. But answers here should fit the algorithm they're requesting, because any other assumptions are likely bad.

    --
    [ e d @ h a l l e y . c c ]

      I get your point. My first mistake was not understanding the original poster's question - even after a few tries. My second mistake was replying whilst damn well knowing I cannot code very well yet.
      I must say though that the question specified a file of 10.000 lines. If very much larger, or of unknown length, it would have been disadvisable.
      Hope I cleared things up ;)

      1
        My first mistake was not understanding the original poster's question - even after a few tries. My second mistake was replying whilst damn well knowing I cannot code very well yet.
        I don't think those are really mistakes in this forum. This community should be a (mostly) judgement-free zone, and mistakes are allowed, and should be encouraged. That's how we learn. (Imagine if all posts had to be peer-reviewed by 3 saints?)

        On the other hand, "I don't know" is an OK response (though you might catch some flack for it). "I don't understand ..." is better.

        "I'm not sure, but try this..." is excellent, IMO. It shows you've put some effort into a response, and you'd like to know how it measures up. That response turns an ordinary question into The Quiz of The Day, stimulating others to join in the fray.

        So don't be afraid to respond.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of