CSVUtils/csv_intrange.pm

filter: Statementstmt 44.4% 20 / 45 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod n/a totaltotal 43.8% 25 / 57 CC 6 SCAR 21.0
CC
6
Cov
41%
__ANON__
20.0
__ANON__
6.5
__ANON__
2.0
CRAP 8.2
« CSVUtils/csv_concat.pm ↑ Summary CSVUtils/csv_sort_fields.pm »
1 package App::CSVUtils::csv_intrange;
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_intrange',
18 summary => 'Output a summary row which are range notation of numbers',
19 description => <<'MARKDOWN',
20  
21 You can use this to check whether integer values in a field form a contiguous
22 range.
23  
24 Non-numbers will be assumed to be 0.
25  
26 Example:
27  
28 # products.csv
29 name,sku_number
30 foo,1
31 bar,2
32 baz,3
33 qux,4
34  
35 % csv-intrange products.csv
36 name,sku_number
37 0,1..4
38  
39 # products2.csv
40 name,sku_number
41 foo,1
42 bar,2
43 baz,3
44 qux,4
45 quux,6
46 corge,6
47  
48 % csv-intrange products2 --with-data-row
49 name,sku_number
50 foo,1
51 bar,2
52 baz,3
53 qux,4
54 quux,6
55 corge,6
56 0,"1..4,6"
57  
58 MARKDOWN
59 add_args => {
60 %App::CSVUtils::argspecopt_with_data_rows,
61 sort => {
62 summary => 'Sort the values first',
63 schema => 'true*',
64 description => <<'MARKDOWN',
65  
66 Sort is done numerically, in ascending order.
67  
68 If you want only certain fields sorted, you can use <prog:csv-sort-rows> first
69 and pipe the result.
70  
71 MARKDOWN
72 },
73 },
74  
75 tags => ['category:summarizing'],
76  
77 0 ▶ on_input_header_row => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 my $r = shift;
79  
80 # we add this key to the stash
81 0 $r->{field_values} = [map {[]} @{$r->{input_fields}}];
82  
83 # because input_* will be cleared by the time of after_read_input,
84 # we save and set it now
85 0 $r->{output_fields} = $r->{input_fields};
86 },
87  
88 0 ▶ on_input_data_row => sub {
Subroutine __ANON__: not called CC=3 SCAR=18.7
89 0 my $r = shift;
90  
91 0 for my $j (0 .. $#{ $r->{input_fields} }) {
92 1 ▶ no warnings 'numeric', 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 0 push @{ $r->{field_values}[$j] }, $r->{input_row}[$j]+0;
94 }
95 0 ▶ $r->{code_print_row}->($r->{input_row}) if $r->{util_args}{with_data_rows};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"util_args"}{"with_data_rows"} 0 0 0
96 },
97  
98 0 ▶ after_read_input => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
99 0 require Number::Util::Range;
100  
101 0 my $r = shift;
102  
103 0 ▶ if ($r->{util_args}{sort}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"sort"}) 0 0 0
104 0 for my $j (0 .. $#{ $r->{output_fields} }) {
105 0 $r->{field_values}[$j] = [ sort { $a <=> $b } @{ $r->{field_values}[$j] } ];
106 }
107 }
108  
109 0 my @ranges;
110 0 for my $j (0 .. $#{ $r->{output_fields} }) {
111 0 push @ranges, join(",", @{ Number::Util::Range::convert_number_sequence_to_range(array => $r->{field_values}[$j], ignore_duplicates=>1) });
112 }
113  
114 0 $r->{code_print_row}->(\@ranges);
115 },
116 1 );
117  
118 1 1;
119 # ABSTRACT: Output a summary row which are range notation of numbers
120  
121 __END__
« CSVUtils/csv_concat.pm ↑ Summary CSVUtils/csv_sort_fields.pm »