Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Further to my last post here is an implemetation of the halve the difference method. Uncomment the 3 lines at the start to generate an 8MB test.file. We find our desired reference point in 20 tries (worst case scenario) in a few milliseconds and then dump the rest of the file (3 lines). Assuming you are going to have to work with dates you will need to modify this of course so you can compare if you are before or after your desired start but the principle holds. The total run time should be only a fraction over the time it take to write your output file. Rename it and you are done. You *will* get an infinite loop if your $find_this is not in the file so we abort if $count > $max_tries. With $maxtries set to 100 you are ok for a file with up to 2**100 lines (10**30 in rough terms :-)

my $file = 'c:/test.file'; #open F, ">$file" or $!; #print F "$_\n" for 1..1000000; #exit; my $find_this = 999997; my $file_size = -s $file; my $top = 0; my $bot = $file_size; my $count = 0; my $max_tries = 100; open OLD, $file or die $!; while (++$count) { my $middle = int(($top+$bot)/2); seek OLD, $middle , 0; my $partial = <OLD>; my $full_line = <OLD>; chomp $full_line; if ($full_line eq $find_this) { print "Took $count tries\n"; print while <OLD>; exit; } if ($full_line < $find_this) { $top = $middle; } else { $bot = $middle; } die "Ark, baling out of infinite loop" if $count > $max_tries; }

Let us know how you get on.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Halve the difference by tachyon
in thread Removing old records from log files by claree0

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Eily]: started*
    [ambrus]: LanX: native speakers are close, but they aren't on the top. there are a _few_ foreigners who have such a thick accent that it takes more time to get used to it and understand the words behind than even the worst native speaker accents.
    [ambrus]: but yes, typically native English speakers, who think that English is still their exclusive property, are the worst.
    [LanX]: Eily: do you live in an English speaking country?
    [LanX]: I think foreigners tend to simplify their phrases, whereby often native speakers don't know how to adapt
    [Eily]: nope, France
    [LanX]: That's pretty true for every language
    [LanX]: so don't worry, anyway most spoken language in the world is broken English
    LanX (especially in the streets of Singapore)
    [Eily]: yes I know, native English speakers are actually a minority among English speakers :)

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (12)
    As of 2017-03-27 16:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should Pluto Get Its Planethood Back?



      Results (320 votes). Check out past polls.