CSVUtils/csv_freqtable.pm

filter: Statementstmt 21.7% 17 / 78 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 4 / 8 Podpod n/a totaltotal 16.5% 21 / 127 CC 21 SCAR 43.4
CC
21
Cov
15%
__ANON__
156.0
__ANON__
30.0
__ANON__
20.0
CRAP 76.5
« CSVUtils/csv_check_cell_values.pm ↑ Summary CSVUtils/csv_get_cells.pm »
1 package App::CSVUtils::csv_freqtable;
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 compile_eval_code
15 eval_code
16 1 );
17  
18 gen_csv_util(
19 name => 'csv_freqtable',
20 summary => 'Output a frequency table of values of a specified field in CSV',
21 description => <<'MARKDOWN',
22  
23 MARKDOWN
24  
25 add_args => {
26 %App::CSVUtils::argspecopt_field_1,
27 ignore_case => {
28 summary => 'Ignore case',
29 schema => 'true*',
30 cmdline_aliases => {i=>{}},
31 },
32 key => {
33 summary => 'Generate computed field with this Perl code',
34 description => <<'MARKDOWN',
35  
36 If specified, then will compute field using Perl code.
37  
38 The code will receive the row (arrayref, or if -H is specified, hashref) as the
39 argument. It should return the computed field (str).
40  
41 MARKDOWN
42 schema => $App::CSVUtils::sch_req_str_or_code,
43 cmdline_aliases => {k=>{}},
44 },
45 %App::CSVUtils::argspecopt_hash,
46 %App::CSVUtils::argspecopt_with_data_rows,
47 },
48 add_args_rels => {
49 'req_one&' => [ ['field', 'key'] ],
50 },
51 tags => ['category:summarizing', 'outputs-data-structure', 'accepts-code'],
52  
53 examples => [
54 {
55 summary => 'Show the age distribution of people',
56 argv => ['people.csv', 'age'],
57 test => 0,
58 'x.doc.show_result' => 0,
59 },
60 {
61 summary => 'Show the frequency of wins by a user, ignore case differences in user',
62 argv => ['winner.csv', 'user', '-i'],
63 test => 0,
64 'x.doc.show_result' => 0,
65 },
66 {
67 summary => 'Show the frequency of events by period (YYYY-MM)',
68 argv => ['events.csv', '-H', '--key', 'sprintf("%04d-%02d", $_->{year}, $_->{month})'],
69 test => 0,
70 'x.doc.show_result' => 0,
71 },
72 ],
73  
74 0 ▶ on_input_header_row => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
75 0 my $r = shift;
76  
77 # check arguments
78 0 my $field_idx;
79 0 ▶ if (defined $r->{util_args}{field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{"util_args"}{"field"}) 0 0 0
80 0 $field_idx = $r->{input_fields_idx}{ $r->{util_args}{field} };
81 0 ▶ die [404, "Field '$r->{util_args}{field}' not found in CSV"]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field_idx 0 0 0
82 unless defined $field_idx;
83 }
84  
85 0 ▶ $r->{wants_input_row_as_hashref} = 1 if $r->{util_args}{hash};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"util_args"}{"hash"} 0 0 0
86  
87 # this is a key we add to the stash
88 0 ▶ $r->{freqtable} //= {};
Condition0%0/2
$r->{"freqtable"} //= {}
l!l
00
MC/DC0%0/1
$r->{"freqtable"} //= {}
$r->{"freqtable"}
Truth table0%0/2
A
A= $r->{"freqtable"}
Aresult
11
00
89 0 $r->{field_idx} = $field_idx;
90 0 $r->{code} = undef;
91 0 $r->{has_added_field} = 0;
92 0 $r->{freq_field} = undef;
93 0 $r->{input_rows} = [];
94 },
95  
96 0 ▶ on_input_data_row => sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
97 0 my $r = shift;
98  
99 # add freq field
100 0 ▶ if ($r->{util_args}{with_data_rows} && !$r->{has_added_field}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"with_data_rows"} and not $r->{"has_added_field"}++) 0 0 0
Condition0%0/3
$r->{"util_args"}{"with_data_rows"} and not $r->{"has_added_field"}++
!ll&&!rl&&r
000
MC/DC0%0/2
$r->{"util_args"}{"with_data_rows"} and not $r->{"has_added_field"}++
$r->{"util_args"}{"with_data_rows"} not $r->{"has_added_field"}++
Truth table0%0/3
A and not(B)
A= $r->{"util_args"}{"with_data_rows"}B= not $r->{"has_added_field"}++
ABresult
0X0
100
111
101 0 my $i = 1;
102 0 while (1) {
103 0 ▶ my $field = "freq" . ($i>1 ? $i : "");
Branch: 1 branch
BranchTrueFalseTotal
$i > 1 ? : 0 0 0
104 0 ▶ unless (defined $r->{input_fields_idx}{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r->{"input_fields_idx"}{$field}) 0 0 0
105 0 $r->{input_fields_idx}{$field} = @{ $r->{input_fields} };
106 0 push @{ $r->{input_fields} }, $field;
107 0 $r->{freq_field} = $field;
108 0 push @{ $r->{input_row} }, undef;
109 0 last;
110 }
111 0 $i++;
112 }
113 }
114  
115 0 my $field_val;
116 0 ▶ if ($r->{util_args}{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'util_args'}{'key'}) { } 0 0 0
117 0 ▶ unless ($r->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"code"}) 0 0 0
118 0 $r->{code} = compile_eval_code($r->{util_args}{key}, 'key');
119 }
120 0 ▶ $field_val = eval_code($r->{code}, $r, $r->{wants_input_row_as_hashref} ? $r->{input_row_as_hashref} : $r->{input_row}) // '';
Branch: 1 branch
BranchTrueFalseTotal
$r->{'wants_input_row_as_hashref'} ? : 0 0 0
Condition0%0/2
eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"}) // ""
l!l
00
MC/DC0%0/1
eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"}) // ""
eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"})
Truth table0%0/2
A
A= eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"})
Aresult
11
00
121 } else {
122 0 $field_val = $r->{input_row}[ $r->{field_idx} ];
123 }
124  
125 0 ▶ if ($r->{util_args}{ignore_case}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"ignore_case"}) 0 0 0
126 0 $field_val = lc $field_val;
127 }
128  
129 0 $r->{freqtable}{$field_val}++;
130  
131 0 ▶ if ($r->{util_args}{with_data_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"with_data_rows"}) 0 0 0
132 # we first put the field val, later we will fill the freq
133 0 ▶ if ($r->{wants_input_row_as_hashref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'wants_input_row_as_hashref'}) { } 0 0 0
134 0 $r->{input_row}{ $r->{freq_field} } = $field_val;
135 } else {
136 0 $r->{input_row}[-1] = $field_val;
137 }
138 0 push @{ $r->{input_rows} }, $r->{input_row};
139 }
140 },
141  
142 writes_csv => 1,
143  
144 0 ▶ after_close_input_files => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
145 0 my $r = shift;
146  
147 0 ▶ if ($r->{util_args}{with_data_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"with_data_rows"}) 0 0 0
148 0 for my $row (@{ $r->{input_rows} }) {
149 0 ▶ if ($r->{wants_input_row_as_hashref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'wants_input_row_as_hashref'}) { } 0 0 0
150 0 my $field_val = $row->{ $r->{freq_field} };
151 0 $row->{ $r->{freq_field} } = $r->{freqtable}{ $field_val };
152 } else {
153 0 my $field_val = $row->[-1];
154 0 $row->[-1] = $r->{freqtable}{ $field_val };
155 }
156 0 $r->{code_print_row}->($row);
157 }
158 }
159 },
160  
161 0 ▶ on_end => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
162 0 my $r = shift;
163  
164 0 ▶ if ($r->{util_args}{with_data_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'util_args'}{'with_data_rows'}) { } 0 0 0
165 0 $r->{result} = [200];
166 } else {
167 0 my @freqtable;
168 0 for (sort { $r->{freqtable}{$b} <=> $r->{freqtable}{$a} } keys %{$r->{freqtable}}) {
169 0 push @freqtable, [$_, $r->{freqtable}{$_}];
170 }
171 0 $r->{result} = [200, "OK", \@freqtable, {'table.fields'=>['value','freq']}];
172 }
173 },
174 1 );
175  
176 1 1;
177 # ABSTRACT: Output a frequency table of values of a specified field in CSV
178  
179 __END__
« CSVUtils/csv_check_cell_values.pm ↑ Summary CSVUtils/csv_get_cells.pm »