CSVUtils/csv_select_rows.pm

filter: Statementstmt 100% 29 / 29 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 41 / 41 CC 5 SCAR 8.8
CC
5
Cov
100%
__ANON__
4.0
__ANON__
2.0
BEGIN
1.0
CRAP 2.4
« CSVUtils/csv_map.pm ↑ Summary CSVUtils/csv_shuf_fields.pm »
1 package App::CSVUtils::csv_select_rows;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2026-07-09'; # DATE
9 2 our $DIST = 'App-CSVUtils'; # DIST
10 2 our $VERSION = '1.038'; # VERSION
11  
12 2 ▶use App::CSVUtils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 gen_csv_util
14 compile_eval_code
15 2 );
16  
17 gen_csv_util(
18 name => 'csv_select_rows',
19 summary => 'Select (only output) rows based on a specification of row numbers (e.g. 2-5,7)',
20 description => <<'MARKDOWN',
21  
22 To select rows by Perl code, see <prog:csv-grep>.
23  
24 MARKDOWN
25 add_args => {
26 rownum_spec => {
27 schema => 'str*',
28 summary => 'Row number (e.g. 1 for first data row), '.
29 'range (3-7), or comma-separated list of such (3-7,10,20-23)',
30 req => 1,
31 pos => 1,
32 },
33 },
34 tags => ['category:filtering'],
35  
36 examples => [
37 {
38 summary => 'Only show rows 1-3 (first to third data rows)',
39 argv => ['file.csv', '1-3'],
40 test => 0,
41 'x.doc.show_result' => 0,
42 },
43 ],
44 links => [
45 {url=>'prog:csvgrep'},
46 ],
47  
48 5 ▶ on_input_header_row => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
49 5 my $r = shift;
50  
51 5 my $spec = $r->{util_args}{rownum_spec};
52 5 my @codestr;
53 5 for my $spec_item (split /\s*,\s*/, $spec) {
54 6 ▶ if ($spec_item =~ /\A\d+\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec_item =~ /\A\d+\z/) { } 3 3 6
elsif ($spec_item =~ /\A(\d+)\s*-\s*(\d+)\z/) { } 2 1 3
55 3 push @codestr, "(\$main::i == $spec_item)";
56 } elsif ($spec_item =~ /\A(\d+)\s*-\s*(\d+)\z/) {
57 2 push @codestr, "(\$main::i >= $1 && \$main::i <= $2)";
58 } else {
59 1 die [400, "Invalid rownum specification '$spec_item'"];
60 }
61 }
62  
63 # we add the following keys to the stash
64 4 $r->{code} = compile_eval_code(join(" || ", @codestr), 'from rownum_spec');
65 },
66  
67 24 ▶ on_input_data_row => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 24 my $r = shift;
69  
70 24 local $main::i = $r->{input_data_rownum};
71 24 ▶ $r->{code_print_row}->($r->{input_row}) if $r->{code}->();
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"code"}->() 8 16 24
72 },
73 2 );
74  
75 2 1;
76 # ABSTRACT: Select (only output) rows based on a specification of row numbers (e.g. 2-5,7)
77  
78 __END__
« CSVUtils/csv_map.pm ↑ Summary CSVUtils/csv_shuf_fields.pm »