fadingjava has asked for the wisdom of the Perl Monks concerning the following question:
#!/user/bin/perl for ($i = 0; $i<= 10; $i++){ $roll = int (rand 453) + 1; push (@dvdr, $roll); } $filename = 'dvd_subtitles_final.txt'; open (DVD, "< /home/sid/kwicionary/$filename") or die "Can't open $fil +ename for reading: $!"; $path = "/home/sid/kwicionary/"; $indexname = "$filename.index"; sysopen(IDX,$indexname, O_CREAT|O_RDWR) or die "Can't open $indexname +for read/write:$!"; build_index(*DVD, *IDX) if -z $indexname; foreach (@dvdr){ $line_number = 2; $line = line_with_index(*ORIG,*IDX,$line_number); die "Didn't find line $line_number in $filename" unless defined $line; print "$_ $line \n"; } sub build_index { my $data_file = shift; my $index_file = shift; my $offset = 0; while (<$data_file>) { print $index_file pack("N", $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("N", 0)); $i_offset = $size * ($line_number-1); seek($index_file, $i_offset, 0) or return; read($index_file, $entry, $size); $d_offset = unpack("N", $entry); seek($data_file, $d_offset, 0); return scalar(<$data_file>); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: reading a particular line in a large text file
by jimbojones (Friar) on Oct 15, 2004 at 22:10 UTC | |
|
Re: reading a particular line in a large text file
by TheEnigma (Pilgrim) on Oct 15, 2004 at 20:28 UTC | |
by fadingjava (Acolyte) on Oct 15, 2004 at 20:40 UTC | |
|
Re: reading a particular line in a large text file
by jimbojones (Friar) on Oct 15, 2004 at 22:14 UTC | |
by fadingjava (Acolyte) on Oct 15, 2004 at 22:28 UTC | |
by jimbojones (Friar) on Oct 16, 2004 at 02:42 UTC | |
by fadingjava (Acolyte) on Oct 16, 2004 at 03:24 UTC | |
|
Re: reading a particular line in a large text file
by TedPride (Priest) on Oct 16, 2004 at 05:24 UTC |