>>>>> "CT" == Chris Thorpe <[EMAIL PROTECTED]> writes:

  CT>   Thanks for the crash course on big-o notation.

schwern and i will send you a bill. :)


  CT>   My point remains that you have to read the file 1 1/2 times or keep an
  CT> array in memory.  There is no way to use byte offsets to find the center
  CT> line in a file because linebreaks are simply a special class of character,
  CT> and are not distributed with any predictability.  If the TELL function

not if you read from the front and back as schwern's code tried to
do. then you can check the tell values until you reach the middle. the
only runtime issue would be if you had enormous lines such as a file
with 1 long line, then you are reading the entire file twice. actually
if you read the backwards line first, then compare it to the foward
tell, then it would read it once for that worst case as both tell values
would be 0. so the rough code would be:

        open F, $file ;                         # forwards
        tie File::ReadBackwards, B, $file ;     # backwards

        $ftell = tell F ;

        while( $line = <B> ) {

                last if $ftell == tell B ;
                <F> ;
                $ftell = tell F ;
        }

        print $line ;

that should read the file once (or a little more depending on how the
lines and/or middle fall across the block boundaries) and store no more
than 2 blocks worth of text at a time.

uri

-- 
Uri Guttman  ------  [EMAIL PROTECTED]  -------- http://www.stemsystems.com
-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org

Reply via email to