CSVUtils/csv_fill_cells.pm

filter: Statementstmt 100% 79 / 79 Branchbran 97.9% 47 / 48 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 99.3% 144 / 145 CC 30 SCAR 31.6
CC
30
Cov
99%
__ANON__
29.0
__ANON__
2.0
BEGIN
1.0
CRAP 23.6
« CSVUtils/csv_sort_rows.pm ↑ Summary CSVUtils/csv_check_field_names.pm »
1 package App::CSVUtils::csv_fill_cells;
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_fill_cells',
19 summary => 'Create a CSV and fill its cells from supplied values (a 1-column CSV)',
20 description => <<'MARKDOWN',
21  
22 This utility takes values (from cells of a 1-column input CSV), creates an
23 output CSV of specified size, and fills the output CSV in one of several
24 possible ways ("layouts"): left-to-right first then top-to-bottom, or
25 bottom-to-top then left-to-right, etc.
26  
27 Some illustration of the layout:
28  
29 % cat 1-to-100.csv
30 num
31 1
32 2
33 3
34 ...
35 100
36  
37 % csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 ; # default layout is 'left_to_right_then_top_to_bottom'
38 field0,field1,field2,field3,field4,field5,field6,field7,field8,field9
39 1,2,3,4,5,6,7,8,9,10
40 11,12,13,14,15,16,17,18,19,20
41 21,22,23,24,25,26,27,28,29,30
42 ...
43 91,92,93,94,95,96,97,98,99,100
44  
45 % csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout top_to_bottom_then_left_to_right
46 field0,field1,field2,field3,field4,field5,field6,field7,field8,field9
47 1,11,21,31,41,51,61,71,81,91
48 2,12,22,32,42,52,62,72,82,92
49 3,13,23,33,43,53,63,73,83,93
50 ...
51 10,20,30,40,50,60,70,80,90,100
52  
53 % csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout top_to_bottom_then_right_to_left
54 91,81,71,61,51,41,31,21,11,1
55 92,82,72,62,52,42,32,22,12,2
56 93,83,73,63,53,43,33,23,13,3
57 ...
58 100,90,80,70,60,50,40,30,20,10
59  
60 % csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout right_to_left_then_top_to_bottom
61 10,9,8,7,6,5,4,3,2,1
62 20,19,18,17,16,15,14,13,12,11
63 30,29,28,27,26,25,24,23,22,21
64 ...
65 100,99,98,97,96,95,94,93,92,91
66  
67 Some additional options are available, e.g.: a filter to let skip filling some
68 cells.
69  
70 When there are more input values than can be fitted, the extra input values are
71 not placed into the output CSV.
72  
73 When there are less input values to fill the specified number of rows, then only
74 the required number of rows and/or columns will be used.
75  
76 Additional options planned:
77  
78 - what to do when there are less values to completely fill the output CSV
79 (whether to always expand or expand when necessary, which is the default).
80  
81 - what to do when there are more values (extend the table or ignore the extra
82 input values, which is the default).
83  
84 MARKDOWN
85 add_args => {
86 # TODO
87 #fields => $App::CSVUtils::argspecopt_fields{fields}, # category:output
88  
89 layout => {
90 summary => 'Specify how the output CSV is to be filled',
91 schema => ['str*', in=>[
92 'left_to_right_then_top_to_bottom',
93 'right_to_left_then_top_to_bottom',
94 'left_to_right_then_bottom_to_top',
95 'right_to_left_then_bottom_to_top',
96 'top_to_bottom_then_left_to_right',
97 'top_to_bottom_then_right_to_left',
98 'bottom_to_top_then_left_to_right',
99 'bottom_to_top_then_right_to_left',
100 ]],
101 default => 'left_to_right_then_top_to_bottom',
102 tags => ['category:layout'],
103 },
104  
105 filter => {
106 summary => 'Code to filter cells to fill',
107 schema => 'str*',
108 description => <<'MARKDOWN',
109  
110 Code will be compiled in the `main` package.
111  
112 Code is passed `($r, $output_row_num, $output_field_idx)` where `$r` is the
113 stash, `$output_row_num` is a 1-based integer (first data row means 1), and
114 `$output_field_idx` is the 0-based field index (0 means the first index). Code
115 is expected to return a boolean value, where true meaning the cell should be
116 filied.
117  
118 MARKDOWN
119 tags => ['category:filtering'],
120 },
121 num_rows => {
122 summary => 'Number of rows of the output CSV',
123 schema => 'posint*',
124 req => 1,
125 tags => ['category:output'],
126 },
127 num_fields => {
128 summary => 'Number of fields of the output CSV',
129 schema => 'posint*',
130 req => 1,
131 tags => ['category:output'],
132 },
133 },
134  
135 tags => ['category:generating', 'accepts-code'],
136  
137 examples => [
138 {
139 summary => 'Fill number 1..100 into a 10x10 grid',
140 src => q{seq 1 100 | [[prog]] --num-rows 10 --num-fields 10},
141 src_plang => 'bash',
142 test => 0,
143 },
144 ],
145  
146 9 ▶ on_input_header_row => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
147 9 my $r = shift;
148  
149 # set output fields
150 27 $r->{output_fields} = [ map {"field$_"}
151 9 0 .. $r->{util_args}{num_fields}-1 ];
152  
153 # compile filter
154 9 ▶ if ($r->{util_args}{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"filter"}) 1 8 9
155 1 my $code = compile_eval_code($r->{util_args}{filter}, 'filter');
156 # this is a key we add to the stash
157 1 $r->{filter} = $code;
158 }
159  
160 # this is a key we add to the stash
161 9 $r->{input_values} = [];
162 },
163  
164 81 ▶ on_input_data_row => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 81 my $r = shift;
166  
167 81 push @{ $r->{input_values} }, $r->{input_row}[0];
168 },
169  
170 9 ▶ after_read_input => sub {
Subroutine __ANON__: called CC=29 SCAR=33.7
171 9 my $r = shift;
172  
173 9 my $i = -1;
174 9 ▶ my $layout = $r->{util_args}{layout} // 'left_to_right_then_top_to_bottom';
Condition100%2/2
$r->{"util_args"}{"layout"} // "left_to_right_then_top_to_bottom"
l!l
72
MC/DC100%1/1
$r->{"util_args"}{"layout"} // "left_to_right_then_top_to_bottom"
$r->{"util_args"}{"layout"}
Truth table100%2/2
A
A= $r->{"util_args"}{"layout"}
Aresult
11
00
175 9 my $output_rows = [];
176  
177 9 ▶ my $x = $layout =~ /left_to_right/ ? 0 : $r->{util_args}{num_fields}-1;
Branch: 1 branch
BranchTrueFalseTotal
$layout =~ /left_to_right/ ? : 5 4 9
178 9 ▶ my $y = $layout =~ /top_to_bottom/ ? 1 : $r->{util_args}{num_rows};
Branch: 1 branch
BranchTrueFalseTotal
$layout =~ /top_to_bottom/ ? : 5 4 9
179 9 while (1) {
180 81 ▶ goto INC_POS if $r->{filter} && !$r->{filter}->($r, $y, $x);
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"filter"} and not $r->{"filter"}->($r, $y, $x) 2 79 81
Condition100%3/3
$r->{"filter"} and not $r->{"filter"}->($r, $y, $x)
!ll&&!rl&&r
7272
MC/DC100%2/2
$r->{"filter"} and not $r->{"filter"}->($r, $y, $x)
$r->{"filter"} not $r->{"filter"}->($r, $y, $x)
Truth table100%3/3
A and not(B)
A= $r->{"filter"}B= not $r->{"filter"}->($r, $y, $x)
ABresult
0X0
100
111
181  
182 79 INC_I:
183 $i++;
184 79 ▶ last if $i >= @{ $r->{input_values} };
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @{$r->{"input_values"};} 0 79 79
185  
186 FILL_CELL:
187 79 for (1 .. $y) {
188 157 ▶ $output_rows->[$_-1] //= [map {undef} 1 .. $r->{util_args}{num_fields}];
Condition100%2/2
$$output_rows[$_ - 1] //= [map({undef;} 1 .. $r->{"util_args"}{"num_fields"})]
l!l
13027
MC/DC100%1/1
$$output_rows[$_ - 1] //= [map({undef;} 1 .. $r->{"util_args"}{"num_fields"})]
$$output_rows[$_ - 1]
Truth table100%2/2
A
A= $$output_rows[$_ - 1]
Aresult
11
00
189 }
190 79 $output_rows->[$y-1][$x] = $r->{input_values}[$i];
191  
192 INC_POS:
193 81 ▶ if ($layout =~ /\A(top|bottom)_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout =~ /\A(top|bottom)_/) { } 36 45 81
194 # vertically first then horizontally
195 36 ▶ if ($layout =~ /top_to_bottom/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout =~ /top_to_bottom/) { } 18 18 36
196 18 $y++;
197 18 ▶ if ($y > $r->{util_args}{num_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $r->{"util_args"}{"num_rows"}) 6 12 18
198 6 $y = 1;
199 6 ▶ if ($layout =~ /left_to_right/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout =~ /left_to_right/) { } 3 3 6
200 3 $x++;
201 3 ▶ last if $x >= $r->{util_args}{num_fields};
Branch: 1 branch
BranchTrueFalseTotal
if $x >= $r->{"util_args"}{"num_fields"} 1 2 3
202 } else {
203 3 $x--;
204 3 ▶ last if $x < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 1 2 3
205 }
206 }
207 } else {
208 18 $y--;
209 18 ▶ if ($y < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 1) 6 12 18
210 6 $y = $r->{util_args}{num_rows};
211 6 ▶ if ($layout =~ /left_to_right/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout =~ /left_to_right/) { } 3 3 6
212 3 $x++;
213 3 ▶ last if $x >= $r->{util_args}{num_fields};
Branch: 1 branch
BranchTrueFalseTotal
if $x >= $r->{"util_args"}{"num_fields"} 1 2 3
214 } else {
215 3 $x--;
216 3 ▶ last if $x < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 1 2 3
217 }
218 }
219 }
220 } else {
221 # horizontally first then vertically
222 45 ▶ if ($layout =~ /left_to_right/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout =~ /left_to_right/) { } 27 18 45
223 27 $x++;
224 27 ▶ if ($x >= $r->{util_args}{num_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $r->{"util_args"}{"num_fields"}) 9 18 27
225 9 $x = 0;
226 9 ▶ if ($layout =~ /top_to_bottom/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout =~ /top_to_bottom/) { } 6 3 9
227 6 $y++;
228 6 ▶ last if $y > $r->{util_args}{num_rows};
Branch: 1 branch
BranchTrueFalseTotal
if $y > $r->{"util_args"}{"num_rows"} 2 4 6
229 } else {
230 3 $y--;
231 3 ▶ last if $y < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $y < 1 1 2 3
232 }
233 }
234 } else {
235 18 $x--;
236 18 ▶ if ($x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0) 6 12 18
237 6 $x = $r->{util_args}{num_fields}-1;
238 6 ▶ if ($layout =~ /top_to_bottom/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout =~ /top_to_bottom/) { } 3 3 6
239 3 $y++;
240 3 ▶ last if $y > $r->{util_args}{num_rows};
Branch: 1 branch
BranchTrueFalseTotal
if $y > $r->{"util_args"}{"num_rows"} 1 2 3
241 } else {
242 3 $y--;
243 3 ▶ last if $y < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $y < 1 1 2 3
244 }
245 }
246 }
247 }
248 }
249  
250 # print rows
251 9 for my $row (@$output_rows) {
252 27 $r->{code_print_row}->($row);
253 }
254 },
255 2 );
256  
257 2 1;
258 # ABSTRACT: Create a CSV and fill its cells from supplied values (a 1-column CSV)
259  
260 __END__
« CSVUtils/csv_sort_rows.pm ↑ Summary CSVUtils/csv_check_field_names.pm »