I change the pack parameter from "N" to "L", maybe the "L" could be enough to store the line number, while the "N" is not long enough.

Both "N" and "L" are 32-bit usigned integers, so that's not going to make a (useful) difference.  They only differ with respect to byte order:

A 32-bit unsigned int can hold values up to 4294967296 (4G). If you need to store larger values, you could use the "Q" template (64-bit), if your build of perl supports it.  Otherwise - or if you want to save space - you could just "add" another single byte ("C"), so you have 5 bytes / 40-bit in total — which would be able to handle indices of up to around 1 Terabyte:

my $i = 78187493530; write_index($i); print read_index(); # 78187493530 sub write_index { my $i = shift; open my $f, ">", "myindex" or die $!; my $pi = pack("CN", $i / 2**32, $i % 2**32); print $f $pi; # writes 5 bytes close $f; } sub read_index { open my $f, "<", "myindex" or die $!; read $f, my $pi, 5; my ($C, $N) = unpack "CN", $pi; return $C * 2**32 + $N; }

This works even with 32-bit-int perls, because then numbers larger than 32-bit are handled as floats internally.


In reply to Re^3: index for large text file by Eliya
in thread index for large text file by cafeblue

Title:
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, details, 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, summary, 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.