Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

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

Mandatory background story

I had an end-of-day job that was taking too much time, and it turns out that the problem was retrieving the closing time values from several textual log files, using a sequential scan.

I knew the answer was to do a binary search, but I was fairly certain someone else HAD to have run into this problem before, so I typed text file binary search in the Search box here, and got Binary Searches on Sorted Text Files.

runrig's reply in that thread pointed me to File::SortedSeek.

Module overview

File::SortedSeek implements binary searches through large sorted text files, using string, numeric, or timestamp keys.

It's ideal for finding a particular time in large logs, for instance.

The module was authored by Dr. James Freeman, better known around here as tachyon.

Usage example

The files I have to deal with use a non-standard integer timestamp. Each line looks like

{timestamp} {value} {value} {value} {value}

Using the module to find the right line couldn't be easier!

#!/usr/bin/perl -w use strict; use File::SortedSeek ':all'; my $targetTS=1234567789; # for instance open(FILE,"<") or die "Can't open, error $!"; # File::SortedSeek::numeric looks for a numeric key # It takes an optional sub ref argument to "massage" the # line from the file to extract the key. numeric(*FILE,$targetTS,sub {$_[0]=~/^\s*(\d+)/ or return 0; return $1 +}); # this will return the line that matches $targetTS, or the first line +> $line=<FILE>; close(FILE);


tachyon rules!!! :)

In reply to File::SortedSeek by RMGir

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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2017-12-12 18:38 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (335 votes). Check out past polls.