| 1 | package App::CSVUtils::csv_pick_cells; | ||||||||||
| 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_cells', | ||||||||||
| 18 | summary => 'Get one or more random cells from CSV', | ||||||||||
| 19 | description => <<'MARKDOWN', | ||||||||||
| 20 | |||||||||||
| 21 | The values will be returned as a 1-column CSV. | ||||||||||
| 22 | |||||||||||
| 23 | MARKDOWN | ||||||||||
| 24 | add_args => { | ||||||||||
| 25 | num_cells => { | ||||||||||
| 26 | summary => 'Number of cells to pick', | ||||||||||
| 27 | schema => 'posint*', | ||||||||||
| 28 | default => 1, | ||||||||||
| 29 | cmdline_aliases => {n=>{}}, | ||||||||||
| 30 | }, | ||||||||||
| 31 | }, | ||||||||||
| 32 | links => [ | ||||||||||
| 33 | {url=>'prog:csv-pick-rows'}, | ||||||||||
| 34 | {url=>'prog:csv-pick-fields'}, | ||||||||||
| 35 | {url=>'prog:csv-fill-cells'}, | ||||||||||
| 36 | ], | ||||||||||
| 37 | tags => ['category:extracting', 'random'], | ||||||||||
| 38 | |||||||||||
| 39 | examples => [ | ||||||||||
| 40 | { | ||||||||||
| 41 | summary => 'Pick a random cell value from CSV', | ||||||||||
| 42 | argv => ['file.csv'], | ||||||||||
| 43 | test => 0, | ||||||||||
| 44 | 'x.doc.show_result' => 0, | ||||||||||
| 45 | }, | ||||||||||
| 46 | { | ||||||||||
| 47 | summary => 'Pick 5 random cell values from CSV, do not output header row', | ||||||||||
| 48 | argv => ['file.csv', '-n5', '--no-output-header'], | ||||||||||
| 49 | test => 0, | ||||||||||
| 50 | 'x.doc.show_result' => 0, | ||||||||||
| 51 | }, | ||||||||||
| 52 | ], | ||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | on_input_header_row => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 55 | 0 | my $r = shift; | |||||||||
| 56 | |||||||||||
| 57 | # we add this key to the stash | ||||||||||
| 58 | 0 | $r->{picked_cells} = []; | |||||||||
| 59 | 0 | $r->{num_cells} = 0; | |||||||||
| 60 | |||||||||||
| 61 | 0 | $r->{output_fields} = ['value']; | |||||||||
| 62 | }, | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | on_input_data_row => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||
| 65 | 0 | my $r = shift; | |||||||||
| 66 | |||||||||||
| 67 | #say "D:input_data_rownum=$r->{input_data_rownum}"; | ||||||||||
| 68 | 0 | ▶ | if ($r->{util_args}{num_cells} == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | for my $i (0 .. $#{ $r->{input_fields} }) { | |||||||||
| 70 | # algorithm from Learning Perl | ||||||||||
| 71 | $r->{picked_cells}[0] = $r->{input_row}[$i] | ||||||||||
| 72 | 0 | ▶ | if rand(++$r->{num_cells}) < 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | } | ||||||||||
| 74 | } else { | ||||||||||
| 75 | 0 | for my $i (0 .. $#{ $r->{input_fields} }) { | |||||||||
| 76 | 0 | $r->{num_cells}++; | |||||||||
| 77 | # algorithm from Learning Perl, modified | ||||||||||
| 78 | 0 | ▶ | if (@{ $r->{picked_cells} } < $r->{util_args}{num_cells}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | # we haven't reached $num_cells, put cell to result in a | ||||||||||
| 80 | # random position | ||||||||||
| 81 | 0 | splice @{ $r->{picked_cells} }, rand(@{ $r->{picked_cells} }+1), 0, $r->{input_row}[$i]; | |||||||||
| 82 | } else { | ||||||||||
| 83 | # we have reached $num_cells, just replace an item randomly, | ||||||||||
| 84 | # using algorithm from Learning Perl, slightly modified | ||||||||||
| 85 | 0 | rand($r->{num_cells}) < @{ $r->{picked_cells} } | |||||||||
| 86 | 0 | ▶ | and splice @{ $r->{picked_cells} }, rand(@{ $r->{picked_cells} }), 1, $r->{input_row}[$i]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | } | ||||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | }, | ||||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | after_read_input => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 93 | 0 | my $r = shift; | |||||||||
| 94 | |||||||||||
| 95 | 0 | for my $value (@{ $r->{picked_cells} }) { | |||||||||
| 96 | 0 | $r->{code_print_row}->([$value]); | |||||||||
| 97 | } | ||||||||||
| 98 | }, | ||||||||||
| 99 | 1 | ); | |||||||||
| 100 | |||||||||||
| 101 | 1 | 1; | |||||||||
| 102 | # ABSTRACT: Get one or more random cells from CSV | ||||||||||
| 103 | |||||||||||
| 104 | __END__ | ||||||||||