Creating indexes for files in Perl

I am currently new to Perl and I came across a problem:

My goal is to create an easy way to access the line of a large file in Perl as much as possible. I created a file consisting of 5 million lines, in each line there is a line number. Then I created my main program, which would have to be able to print any content on a given line. For this, I use two methods that I found on the Internet:

use Config qw( %Config );

my $off_t = $Config{lseeksize} > $Config{ivsize} ? 'F' : 'j';
my $file = "testfile.err";
open(FILE, "< $file")         or die "Can't open $file for reading: $!\n";
open(INDEX, "+>$file.idx")
        or die "Can't open $file.idx for read/write: $!\n";
build_index(*FILE, *INDEX);
my $line = line_with_index(*FILE, *INDEX, 129);
print "$line";

sub build_index {
    my $data_file  = shift;
    my $index_file = shift;
    my $offset     = 0;

    while (<$data_file>) {
        print $index_file pack($off_t, $offset);
        $offset = tell($data_file);
    }
}

sub line_with_index {
    my $data_file   = shift;
    my $index_file  = shift;
    my $line_number = shift;

    my $size;               # size of an index entry
    my $i_offset;           # offset into the index of the entry
    my $entry;              # index entry
    my $d_offset;           # offset into the data file

    $size = length(pack($off_t, 0));
    $i_offset = $size * ($line_number-1);
    seek($index_file, $i_offset, 0) or return;
    read($index_file, $entry, $size);
    $d_offset = unpack($off_t, $entry);
    seek($data_file, $d_offset, 0);
    return scalar(<$data_file>);
}

, , " $line test2.pl 10" ( 566 ) . , , . , .

, , , .

: , : "N" :

my $off_t = $Config{lseeksize} > $Config{ivsize} ? 'F' : 'j';

, 128, 128 . 129 3, .

Edit2: , , 2 , , , , "head" ( 2 ),

!

+4
1

, , Windows:

open(INDEX, "+>$file.idx")
    or die "Can't open $file.idx for read/write: $!\n";
binmode(INDEX);

, - Windows:

print $index_file pack("j", $offset);

Perl 0x0a 0x0d0a. binmode , .

+2

Source: https://habr.com/ru/post/1536793/


All Articles