in reply to Padding search results with context

You should be able to do it in one iteration...
I would use a queue to store the context lines

Start by inspecting each line.

  1. If the date has changed then flush the queue and set $postContext=0;
  2. Inspect for duplicates
    1. If the line is a duplicate (of the latest item seen?), then print out and flush the queue. Set $postContext = $maxPostContext;
    2. Otherwise, add the line to the queue.
  3. If ($postContext) then print and flush the (single) item from the queue, and $postContext--;
  4. If the queue is more than $maxPreContext, then remove a line from the head of the queue.
Repeat until done.

So, basically, the queue saves up a maximum of $maxPreContext lines of context from before the duplication, and you keep printing $maxPostContext lines for context after the duplication. If another duplicate appears quickly, you don't get multiple copies of the same context because the queue has not refilled.

  • Comment on Re: Padding search results with context