Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I have written this kind of tool to scan Apache log files. The actual problem statement was "return the logs starting at XX:XX:XX and running until YY:YY:YY".

I used a binary search to position myself at the last entry with a time-stamp < XX:XX:XX and then started a sequential read until I reached a timestamp > YY:YY:YY. I used seek() and tell() to get/set my position in the file; calculated the mid-point of my range and then looped through until I got with in range. The only really tricky part was remembering to do two reads at the mid-point to get to a full record with a time-stamp in it (tell() and seek() work on bytes not records).

Pseudo-code:

$start=0, $stop=length of file(array) # Now check the entry at $mid for my start/stop condition $again=1 Loop-while($again=1) $again=0 $mid=int(($stop - $start)/2) if (array[$mid] < $beg) $start = $mid $again=1 if (array[$stop] > $end) $stop = $mid $again=1 End Loop
When you fall out of the loop, $start will be the first entry <= $beg and $stop will be the first >= $end.

----
I Go Back to Sleep, Now.

OGB


In reply to Re: Modified Binary Search by Old_Gray_Bear
in thread Modified Binary Search by Limbic~Region

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2021-06-12 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (53 votes). Check out past polls.

    Notices?