CSVUtils/csv_pick_fields.pm

filter: Statementstmt 56.6% 17 / 30 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 4 / 6 Podpod n/a totaltotal 55.2% 21 / 38 CC 3 SCAR 12.5
CC
3
Cov
53%
__ANON__
6.0
__ANON__
6.0
BEGIN
1.0
CRAP 3.5
« CSVUtils/csv_munge_rows.pm ↑ Summary CSVUtils/csv_check_rows.pm »
1 package App::CSVUtils::csv_pick_fields;
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_fields',
18 summary => 'Select one or more random fields from CSV',
19 description => <<'MARKDOWN',
20  
21  
22 MARKDOWN
23 add_args => {
24 num_fields => {
25 summary => 'Number of fields to pick',
26 schema => 'posint*',
27 default => 1,
28 cmdline_aliases => {n=>{}},
29 },
30 },
31 links => [
32 {url=>'prog:csv-pick-rows'},
33 {url=>'prog:csv-pick-cells'},
34 ],
35 tags => ['category:extracting', 'random'],
36  
37 examples => [
38 {
39 summary => 'Pick 2 random fields row from CSV',
40 argv => ['file.csv'],
41 test => 0,
42 'x.doc.show_result' => 0,
43 },
44 ],
45  
46 0 ▶ on_input_header_row => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
47 0 my $r = shift;
48  
49 0 require List::Util;
50 0 my @randomized_fields = List::Util::shuffle(@{ $r->{input_fields} });
51 0 ▶ if ($r->{util_args}{num_fields} < @randomized_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"num_fields"} < @randomized_fields) 0 0 0
52 0 splice @randomized_fields, 0, (@randomized_fields-$r->{util_args}{num_fields});
53 }
54 0 $r->{output_fields} = \@randomized_fields;
55 },
56  
57 0 ▶ on_input_data_row => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
58 0 my $r = shift;
59  
60 0 my $row = [];
61 0 for my $j (0 .. $#{ $r->{output_fields} }) {
62 0 $row->[$j] = $r->{input_row}[ $r->{input_fields_idx}{ $r->{output_fields}[$j] } ];
63 }
64 0 $r->{code_print_row}->($row);
65 },
66 1 );
67  
68 1 1;
69 # ABSTRACT: Select one or more random fields from CSV
70  
71 __END__
« CSVUtils/csv_munge_rows.pm ↑ Summary CSVUtils/csv_check_rows.pm »