| 1 | package App::CSVUtils::csv_pick_rows; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 8 | 1 | our $DATE = '2026-07-09'; # DATE | |||||||||
| 9 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||
| 10 | 1 | our $VERSION = '1.038'; # VERSION | |||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use App::CSVUtils qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | gen_csv_util | ||||||||||
| 14 | 1 | ); | |||||||||
| 15 | |||||||||||
| 16 | gen_csv_util( | ||||||||||
| 17 | name => 'csv_pick_rows', | ||||||||||
| 18 | summary => 'Return one or more random rows from CSV', | ||||||||||
| 19 | description => <<'MARKDOWN', | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | MARKDOWN | ||||||||||
| 23 | add_args => { | ||||||||||
| 24 | num_rows => { | ||||||||||
| 25 | summary => 'Number of rows to pick', | ||||||||||
| 26 | schema => 'posint*', | ||||||||||
| 27 | default => 1, | ||||||||||
| 28 | cmdline_aliases => {n=>{}}, | ||||||||||
| 29 | }, | ||||||||||
| 30 | }, | ||||||||||
| 31 | links => [ | ||||||||||
| 32 | {url=>'prog:csv-pick-fields'}, | ||||||||||
| 33 | {url=>'prog:csv-pick-cells'}, | ||||||||||
| 34 | ], | ||||||||||
| 35 | tags => ['category:extracting', 'random'], | ||||||||||
| 36 | |||||||||||
| 37 | examples => [ | ||||||||||
| 38 | { | ||||||||||
| 39 | summary => 'Pick a random row from CSV', | ||||||||||
| 40 | argv => ['file.csv'], | ||||||||||
| 41 | test => 0, | ||||||||||
| 42 | 'x.doc.show_result' => 0, | ||||||||||
| 43 | }, | ||||||||||
| 44 | { | ||||||||||
| 45 | summary => 'Pick 5 random rows from CSV', | ||||||||||
| 46 | argv => ['file.csv', '-n5'], | ||||||||||
| 47 | test => 0, | ||||||||||
| 48 | 'x.doc.show_result' => 0, | ||||||||||
| 49 | }, | ||||||||||
| 50 | ], | ||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | on_input_header_row => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 53 | 0 | my $r = shift; | |||||||||
| 54 | |||||||||||
| 55 | # we add this key to the stash | ||||||||||
| 56 | 0 | $r->{picked_rows} = []; | |||||||||
| 57 | |||||||||||
| 58 | # because input_* will be cleared by the time of after_read_input, | ||||||||||
| 59 | # we save and set it now | ||||||||||
| 60 | 0 | $r->{output_fields} = $r->{input_fields}; | |||||||||
| 61 | }, | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | on_input_data_row => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||
| 64 | 0 | my $r = shift; | |||||||||
| 65 | |||||||||||
| 66 | #say "D:input_data_rownum=$r->{input_data_rownum}"; | ||||||||||
| 67 | 0 | ▶ | if ($r->{util_args}{num_rows} == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | # algorithm from Learning Perl | ||||||||||
| 69 | 0 | ▶ | $r->{picked_rows}[0] = $r->{input_row} if rand($r->{input_data_rownum}) < 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | } else { | ||||||||||
| 71 | # algorithm from Learning Perl, modified | ||||||||||
| 72 | 0 | ▶ | if (@{ $r->{picked_rows} } < $r->{util_args}{num_rows}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | # we haven't reached $num_rows, put row to result in a random | ||||||||||
| 74 | # position | ||||||||||
| 75 | 0 | splice @{ $r->{picked_rows} }, rand(@{ $r->{picked_rows} }+1), 0, $r->{input_row}; | |||||||||
| 76 | } else { | ||||||||||
| 77 | # we have reached $num_rows, just replace an item randomly, | ||||||||||
| 78 | # using algorithm from Learning Perl, slightly modified | ||||||||||
| 79 | 0 | rand($r->{input_data_rownum}) < @{ $r->{picked_rows} } | |||||||||
| 80 | 0 | ▶ | and splice @{ $r->{picked_rows} }, rand(@{ $r->{picked_rows} }), 1, $r->{input_row}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | }, | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | after_read_input => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 86 | 0 | my $r = shift; | |||||||||
| 87 | |||||||||||
| 88 | 0 | for my $row (@{ $r->{picked_rows} }) { | |||||||||
| 89 | 0 | $r->{code_print_row}->($row); | |||||||||
| 90 | } | ||||||||||
| 91 | }, | ||||||||||
| 92 | 1 | ); | |||||||||
| 93 | |||||||||||
| 94 | 1 | 1; | |||||||||
| 95 | # ABSTRACT: Return one or more random rows from CSV | ||||||||||
| 96 | |||||||||||
| 97 | __END__ | ||||||||||