rand($.) < 1 && ($line = $_) while <>;
#!/usr/bin/perl
use strict;
use warnings;
my $num = shift @ARGV;
my $tgtfn = shift @ARGV;
open(my $fh, "<", $tgtfn) or die;
while (<$fh>) { }
my $line_max = $.;
die if $line_max < $num;
my @lns;
my %seen;
for (my $i = 0; $i < $num; $i++) {
do {
$lns[$i] = int(rand($line_max));
} while (defined $seen{$lns[$i]});
$seen{$lns[$i]} = 1;
}
seek $fh, 0, 0;
$. = 0;
my @res = ();
while (<$fh>) {
for (my $i = 0; $i < $num; $i++) {
next if $. != $lns[$i] + 1;
$res[$i] = $_;
last;
}
}
close $fh;
print @res;
% cat test.dic Japan Tokyo Yokohama This is a pen Hello World % ./getlines-naive.pl 3 test.dic Tokyo This is a pen Yokohama % ./getlines-naive.pl 3 test.dic Yokohama Tokyo Hello World % ./getlines-naive.pl 3 test.dic Japan This is a pen Yokohama
#!/usr/bin/perl
use strict;
use warnings;
my $ip = 0;
while (<>) {
print pack("N", $ip);
$ip += length($_);
}
#!/usr/bin/perl
use strict;
use warnings;
my $len_of_N = 4;
my $num = shift @ARGV;
my $tgtfn = shift @ARGV;
my $idxfn = shift @ARGV || $tgtfn.".ary";
my $tfsz = -s $tgtfn;
my $ifsz = -s $idxfn;
my $line_max = $ifsz / $len_of_N;
die if $line_max < $num;
my @lns;
my %seen;
for (my $i = 0; $i < $num; $i++) {
do {
$lns[$i] = int(rand($line_max));
} while (defined $seen{$lns[$i]});
$seen{$lns[$i]} = 1;
}
open(my $fi, "<", $idxfn) or die;
open(my $fh, "<", $tgtfn) or die;
my @res = ();
for (my $i = 0; $i < $num; $i++) {
my ($ixf, $len) = get_info($fi, $lns[$i]);
my $str = get_string($fh, $ixf, $len);
$res[$i] = $str;
}
close $fi;
close $fh;
print @res;
sub get_info {
my ($fh, $qidx) = @_;
my $buf = "";
seek $fi, $qidx * $len_of_N, 0;
read $fi, $buf, $len_of_N;
my $ixf = unpack("N", $buf);
my $ixt = (tell $fi < $ifsz) ? do {
read $fi, $buf, $len_of_N;
unpack("N", $buf);
} : $tfsz;
return ($ixf, $ixt-$ixf);
}
sub get_string {
my ($fh, $ixf, $len) = @_;
my $buf = "";
seek $fh, $ixf, 0;
read $fh, $buf, $len;
return $buf;
}
% cat test.dic Japan Tokyo Yokohama This is a pen Hello World % ./mkidx.pl test.dic > test.dic.ary % ./getlines.pl 3 test.dic Hello World Yokohama Japan % ./getlines.pl 3 test.dic This is a pen Tokyo Japan % ./getlines.pl 3 test.dic Tokyo Japan Yokohama