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