Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Modified Binary Search

by Old_Gray_Bear (Bishop)
on Jan 13, 2010 at 18:01 UTC ( #817242=note: print w/replies, xml ) Need Help??


in reply to Modified Binary Search

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2021-06-12 22:24 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?