| 1 | package File::Random::Pick; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | our $DATE = '2019-09-15'; # DATE | |||||||||||||
| 4 | 1 | our $VERSION = '0.03'; # VERSION | |||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | our @EXPORT_OK = qw(random_line); | |||||||||||||
| 12 | |||||||||||||||
| 13 | 0 | ▶ | sub random_line { | ||||||||||||
|
Subroutine
random_line: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||
| 14 | 0 | my ($path, $num_lines) = @_; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 0 | ▶ | $num_lines //= 1; | ||||||||||||
|
Condition0%0/2
$num_lines //= 1
MC/DC0%0/1
$num_lines //= 1
$num_lines
Truth table0%0/2
A
A= $num_lines
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 0 | my $fh; | |||||||||||||
| 19 | 0 | ▶ | if (ref($path)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | 0 | $fh = $path; | |||||||||||||
| 21 | } else { | ||||||||||||||
| 22 | 0 | ▶ | open $fh, "<", $path or die "Can't open $path: $!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | } | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 0 | ▶ | if ($num_lines < 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 26 | 0 | die "Please specify a positive number of lines to pick"; | |||||||||||||
| 27 | } elsif ($num_lines == 1) { | ||||||||||||||
| 28 | # use algorithm from Learning Perl | ||||||||||||||
| 29 | 0 | my $line; | |||||||||||||
| 30 | 0 | ▶ | while (<$fh>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 0 | ▶ | $line = $_ if rand($.) < 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | } | ||||||||||||||
| 33 | 0 | return $line; | |||||||||||||
| 34 | } else { | ||||||||||||||
| 35 | 0 | my @lines; | |||||||||||||
| 36 | 0 | ▶ | while (<$fh>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | 0 | ▶ | if (@lines < $num_lines) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | # we haven't reached $num_lines, put line to result in a random | ||||||||||||||
| 39 | # position | ||||||||||||||
| 40 | 0 | splice @lines, rand(@lines+1), 0, $_; | |||||||||||||
| 41 | } else { | ||||||||||||||
| 42 | # we have reached $nnum_items, just replace an item randomly, | ||||||||||||||
| 43 | # using algorithm from Learning Perl, slightly modified | ||||||||||||||
| 44 | 0 | ▶ | rand($.) < @lines and splice @lines, rand(@lines), 1, $_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | } | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | 0 | return @lines; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 1 | 1; | |||||||||||||
| 52 | # ABSTRACT: Pick random lines from a file, without duplicates | ||||||||||||||
| 53 | |||||||||||||||
| 54 | __END__ | ||||||||||||||