CSVUtils/csv_sort_rows.pm

filter: Statementstmt 94.4% 102 / 108 Branchbran 72.5% 45 / 62 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 85.7% 169 / 197 CC 39 SCAR 33.7
CC
39
Cov
87%
after_close_input_files
40.1
on_input_data_row
5.0
on_input_header_row
2.0
CRAP 29.1
« CSVUtils/csv_pick_cells.pm ↑ Summary CSVUtils/csv_fill_cells.pm »
1 package App::CSVUtils::csv_sort_rows;
2  
3 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9 3 our $DATE = '2026-07-09'; # DATE
10 3 our $DIST = 'App-CSVUtils'; # DIST
11 3 our $VERSION = '1.038'; # VERSION
12  
13 3 ▶use App::CSVUtils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 gen_csv_util
15 compile_eval_code
16 3 );
17  
18 14 ▶sub on_input_header_row {
Subroutine on_input_header_row: called CC=2 SCAR=6.9
Pod undocumented
19 14 my $r = shift;
20 14 ▶ $r->{wants_input_row_as_hashref}++ if $r->{util_args}{hash};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"util_args"}{"hash"} 3 11 14
21 }
22  
23 42 ▶sub on_input_data_row {
Subroutine on_input_data_row: called CC=5 SCAR=16.1
Pod undocumented
24 42 my $r = shift;
25  
26 # keys we add to the stash
27 42 ▶ $r->{input_rows} //= [];
Condition100%2/2
$r->{"input_rows"} //= []
l!l
2814
MC/DC100%1/1
$r->{"input_rows"} //= []
$r->{"input_rows"}
Truth table100%2/2
A
A= $r->{"input_rows"}
Aresult
11
00
28 42 ▶ if ($r->{wants_input_row_as_hashref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"wants_input_row_as_hashref"}) 9 33 42
29 9 ▶ $r->{input_rows_as_hashref} //= [];
Condition100%2/2
$r->{"input_rows_as_hashref"} //= []
l!l
63
MC/DC100%1/1
$r->{"input_rows_as_hashref"} //= []
$r->{"input_rows_as_hashref"}
Truth table100%2/2
A
A= $r->{"input_rows_as_hashref"}
Aresult
11
00
30 }
31  
32 42 push @{ $r->{input_rows} }, $r->{input_row};
33 42 ▶ if ($r->{wants_input_row_as_hashref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"wants_input_row_as_hashref"}) 9 33 42
34 9 push @{ $r->{input_rows_as_hashref} }, $r->{input_row_as_hashref};
35 }
36 }
37  
38 14 ▶sub after_close_input_files {
Subroutine after_close_input_files: called CC=34 SCAR=36.9
Pod undocumented
39 14 my $r = shift;
40  
41 # we do the actual sorting here after collecting all the rows
42  
43 # whether we should compute keys
44 14 my @keys;
45 14 ▶ if ($r->{util_args}{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"key"}) 4 10 14
46 4 my $code_gen_key = compile_eval_code($r->{util_args}{key}, 'key');
47 4 ▶ for my $row (@{ $r->{util_args}{hash} ? $r->{input_rows_as_hashref} : $r->{input_rows} }) {
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'hash'} ? : 2 2 4
48 12 local $_ = $row;
49 12 push @keys, $code_gen_key->($row);
50 }
51 }
52  
53 # if user doesn't specify any by_* args, try to set a default if we can
54 14 ▶ if (!(grep {defined} ($r->{util_args}{by_code}, $r->{util_args}{by_sortsub}, $r->{util_args}{by_fields}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {defined $_;} $r->{"util_args"}{"by_code"}, $r->{"util_args"}{"by_sortsub"}, $r->{"util_args"}{"by_fields"}) 0 14 14
55 # if there is only a single field, use it
56 0 ▶ if (@{ $r->{input_fields} } == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$r->{"input_fields"};} == 1) 0 0 0
57 0 my $field = $r->{input_fields}[0];
58 $r->{util_args}{by_fields} = App::CSVUtils::_is_numeric_field(
59 0 ▶ $r->{input_rows}, 0) ? ["+$field"] : [$field];
Branch: 1 branch
BranchTrueFalseTotal
App::CSVUtils::_is_numeric_field($r->{'input_rows'}, 0) ? : 0 0 0
60 }
61 }
62  
63 14 my $sorted_rows;
64 14 ▶ if ($r->{util_args}{by_code} || $r->{util_args}{by_sortsub}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r->{'util_args'}{'by_code'} or $r->{'util_args'}{'by_sortsub'}) { } 6 8 14
elsif ($r->{'util_args'}{'by_fields'}) { } 8 0 8
Condition100%3/3
$r->{"util_args"}{"by_code"} or $r->{"util_args"}{"by_sortsub"}
l!l&&r!l&&!r
428
MC/DC100%2/2
$r->{"util_args"}{"by_code"} or $r->{"util_args"}{"by_sortsub"}
$r->{"util_args"}{"by_code"} $r->{"util_args"}{"by_sortsub"}
Truth table100%3/3
A or B
A= $r->{"util_args"}{"by_code"}B= $r->{"util_args"}{"by_sortsub"}
ABresult
1X1
011
000
65  
66 6 my $code0;
67 6 ▶ if ($r->{util_args}{by_code}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r->{'util_args'}{'by_code'}) { } 4 2 6
elsif (defined $r->{'util_args'}{'by_sortsub'}) { } 2 0 2
68 4 $code0 = compile_eval_code($r->{util_args}{by_code}, 'by_code');
69 } elsif (defined $r->{util_args}{by_sortsub}) {
70 2 require Sort::Sub;
71 $code0 = Sort::Sub::get_sorter(
72 2 $r->{util_args}{by_sortsub}, $r->{util_args}{sortsub_args});
73 }
74  
75 6 my $sort_indices;
76 my $code;
77 6 ▶ if (@keys) {
Branch: 2 branches
BranchTrueFalseTotal
if (@keys) { } 4 2 6
elsif ($r->{'util_args'}{'hash'}) { } 1 1 2
78 # compare two sort keys ($a & $b) are indices
79 4 $sort_indices++;
80 11 ▶ $code = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 11 local $main::a = $keys[$a];
82 11 local $main::b = $keys[$b];
83 #log_trace "a=<$main::a> vs b=<$main::b>";
84 11 $code0->($main::a, $main::b);
85 4 };
86 } elsif ($r->{util_args}{hash}) {
87 1 $sort_indices++;
88 3 ▶ $code = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 3 local $main::a = $r->{input_rows_as_hashref}[$a];
90 3 local $main::b = $r->{input_rows_as_hashref}[$b];
91 #log_trace "a=<%s> vs b=<%s>", $main::a, $main::b;
92 3 $code0->($main::a, $main::b);
93 1 };
94 } else {
95 1 $code = $code0;
96 }
97  
98 6 ▶ if ($sort_indices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_indices) { } 5 1 6
99 5 my @sorted_indices = sort { local $main::a=$a; local $main::b=$b; $code->($main::a,$main::b) } 0..$#{$r->{input_rows}};
100 5 $sorted_rows = [map {$r->{input_rows}[$_]} @sorted_indices];
101 } else {
102 1 $sorted_rows = [sort { local $main::a=$a; local $main::b=$b; $code->($main::a,$main::b) } @{$r->{input_rows}}];
103 }
104  
105 } elsif ($r->{util_args}{by_fields}) {
106  
107 8 my @fields;
108 8 my $code_str = "";
109 8 for my $field_spec (@{ $r->{util_args}{by_fields} }) {
110 9 my ($prefix, $field) = $field_spec =~ /\A([+~-]?)(.+)/;
111 9 my $field_idx = App::CSVUtils::_find_field($r->{input_fields}, $field);
112 9 ▶ $prefix //= "";
Condition50%1/2
$prefix //= ""
l!l
90
MC/DC0%0/1
$prefix //= ""
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
113 9 ▶ if ($prefix eq '+') {
Branch: 4 branches
BranchTrueFalseTotal
if ($prefix eq '+') { } 2 7 9
elsif ($prefix eq '-') { } 2 5 7
elsif ($prefix eq '') { } 4 1 5
elsif ($prefix eq '~') { } 1 0 1
114 2 ▶ $code_str .= ($code_str ? " || " : "") .
Branch: 1 branch
BranchTrueFalseTotal
$code_str ? : 1 1 2
115 "(\$a->[$field_idx] <=> \$b->[$field_idx])";
116 } elsif ($prefix eq '-') {
117 2 ▶ $code_str .= ($code_str ? " || " : "") .
Branch: 1 branch
BranchTrueFalseTotal
$code_str ? : 0 2 2
118 "(\$b->[$field_idx] <=> \$a->[$field_idx])";
119 } elsif ($prefix eq '') {
120 4 ▶ if ($r->{util_args}{ci}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'util_args'}{'ci'}) { } 1 3 4
121 1 ▶ $code_str .= ($code_str ? " || " : "") .
Branch: 1 branch
BranchTrueFalseTotal
$code_str ? : 0 1 1
122 "(lc(\$a->[$field_idx]) cmp lc(\$b->[$field_idx]))";
123 } else {
124 3 ▶ $code_str .= ($code_str ? " || " : "") .
Branch: 1 branch
BranchTrueFalseTotal
$code_str ? : 0 3 3
125 "(\$a->[$field_idx] cmp \$b->[$field_idx])";
126 }
127 } elsif ($prefix eq '~') {
128 1 ▶ if ($r->{util_args}{ci}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'util_args'}{'ci'}) { } 0 1 1
129 0 ▶ $code_str .= ($code_str ? " || " : "") .
Branch: 1 branch
BranchTrueFalseTotal
$code_str ? : 0 0 0
130 "(lc(\$b->[$field_idx]) cmp lc(\$a->[$field_idx]))";
131 } else {
132 1 ▶ $code_str .= ($code_str ? " || " : "") .
Branch: 1 branch
BranchTrueFalseTotal
$code_str ? : 0 1 1
133 "(\$b->[$field_idx] cmp \$a->[$field_idx])";
134 }
135 }
136 }
137 8 my $code = compile_eval_code($code_str, 'from sort_by_fields');
138 8 $sorted_rows = [sort { local $main::a = $a; local $main::b = $b; $code->($main::a, $main::b) } @{$r->{input_rows}}];
139  
140 } else {
141  
142 0 die [400, "Please specify by_fields or by_sortsub or by_code"];
143  
144 }
145  
146 14 ▶ if ($main::_CSV_SORTED_ROWS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::_CSV_SORTED_ROWS) { } 2 12 14
147 2 require Data::Cmp;
148 #use DD; dd $r->{input_rows}; print "\n"; dd $sorted_rows;
149 2 ▶ if (Data::Cmp::cmp_data($r->{input_rows}, $sorted_rows)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Cmp::cmp_data($r->{'input_rows'}, $sorted_rows)) { } 1 1 2
150 # not sorted
151 1 ▶ $r->{result} = [400, "NOT sorted", $r->{util_args}{quiet} ? undef : "Rows are NOT sorted"];
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'quiet'} ? : 0 1 1
152 } else {
153 # sorted
154 1 ▶ $r->{result} = [200, "Sorted", $r->{util_args}{quiet} ? undef : "Rows are sorted"];
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'quiet'} ? : 0 1 1
155 }
156 } else {
157 12 for my $row (@$sorted_rows) {
158 36 $r->{code_print_row}->($row);
159 }
160 }
161 }
162  
163 3 gen_csv_util(
164 name => 'csv_sort_rows',
165 summary => 'Sort CSV rows',
166 description => <<'MARKDOWN',
167  
168 This utility sorts the rows in the CSV. Example input CSV:
169  
170 name,age
171 Andy,20
172 Dennis,15
173 Ben,30
174 Jerry,30
175  
176 Example output CSV (using `--by-field +age` which means by age numerically and
177 ascending):
178  
179 name,age
180 Dennis,15
181 Andy,20
182 Ben,30
183 Jerry,30
184  
185 Example output CSV (using `--by-field -age`, which means by age numerically and
186 descending):
187  
188 name,age
189 Ben,30
190 Jerry,30
191 Andy,20
192 Dennis,15
193  
194 Example output CSV (using `--by-field name`, which means by name ascibetically
195 and ascending):
196  
197 name,age
198 Andy,20
199 Ben,30
200 Dennis,15
201 Jerry,30
202  
203 Example output CSV (using `--by-field ~name`, which means by name ascibetically
204 and descending):
205  
206 name,age
207 Jerry,30
208 Dennis,15
209 Ben,30
210 Andy,20
211  
212 Example output CSV (using `--by-field +age --by-field ~name`):
213  
214 name,age
215 Dennis,15
216 Andy,20
217 Jerry,30
218 Ben,30
219  
220 You can also reverse the sort order (`-r`) or sort case-insensitively (`-i`).
221  
222 For more flexibility, instead of `--by-field` you can use `--by-code`:
223  
224 Example output `--by-code '$a->[1] <=> $b->[1] || $b->[0] cmp $a->[0]'` (which
225 is equivalent to `--by-field +age --by-field ~name`):
226  
227 name,age
228 Dennis,15
229 Andy,20
230 Jerry,30
231 Ben,30
232  
233 If you use `--hash`, your code will receive the rows to be compared as hashref,
234 e.g. `--hash --by-code '$a->{age} <=> $b->{age} || $b->{name} cmp $a->{name}'.
235  
236 A third alternative is to sort using <pm:Sort::Sub> routines. Example output
237 (using `--by-sortsub 'by_length<r>' --key '$_->[0]'`, which is to say to sort by
238 descending length of name):
239  
240 name,age
241 Dennis,15
242 Jerry,30
243 Andy,20
244 Ben,30
245  
246 If none of the `--by-*` options are specified, the utility will bail unless
247 there's a default that can be used, e.g. when CSV has a single field then that
248 field will be used.
249  
250 MARKDOWN
251  
252 add_args => {
253 %App::CSVUtils::argspecopt_hash,
254 %App::CSVUtils::argspecs_sort_rows,
255 },
256  
257 tags => ['category:sorting'],
258  
259 on_input_header_row => \&App::CSVUtils::csv_sort_rows::on_input_header_row,
260  
261 on_input_data_row => \&App::CSVUtils::csv_sort_rows::on_input_data_row,
262  
263 after_close_input_files => \&App::CSVUtils::csv_sort_rows::after_close_input_files,
264  
265 );
266  
267 3 1;
268 # ABSTRACT: Sort CSV rows
269  
270 __END__
« CSVUtils/csv_pick_cells.pm ↑ Summary CSVUtils/csv_fill_cells.pm »