App-CSVUtils
view release on metacpan or search on metacpan
lib/App/CSVUtils/csv_fill_cells.pm view on Meta::CPAN
summary => 'Create a CSV and fill its cells from supplied values (a 1-column CSV)',
description => <<'_',
This utility takes values (from cells of a 1-column input CSV), creates an
output CSV of specified size, and fills the output CSV in one of several
possible ways ("layouts"): left-to-right first then top-to-bottom, or
bottom-to-top then left-to-right, etc.
Some illustration of the layout:
% cat 1-to-100.csv
num
1
2
3
...
100
% csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 ; # default layout is 'left_to_right_then_top_to_bottom'
field0,field1,field2,field3,field4,field5,field6,field7,field8,field9
1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21,22,23,24,25,26,27,28,29,30
...
91,92,93,94,95,96,97,98,99,100
% csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout top_to_bottom_then_left_to_right
field0,field1,field2,field3,field4,field5,field6,field7,field8,field9
1,11,21,31,41,51,61,71,81,91
2,12,22,32,42,52,62,72,82,92
3,13,23,33,43,53,63,73,83,93
...
10,20,30,40,50,60,70,80,90,100
% csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout top_to_bottom_then_right_to_left
91,81,71,61,51,41,31,21,11,1
92,82,72,62,52,42,32,22,12,2
93,83,73,63,53,43,33,23,13,3
...
100,90,80,70,60,50,40,30,20,10
% csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout right_to_left_then_top_to_bottom
10,9,8,7,6,5,4,3,2,1
20,19,18,17,16,15,14,13,12,11
30,29,28,27,26,25,24,23,22,21
...
100,99,98,97,96,95,94,93,92,91
Some additional options are available, e.g.: a filter to let skip filling some
cells.
When there are more input values than can be fitted, the extra input values are
not placed into the output CSV.
When there are less input values to fill the specified number of rows, then only
the required number of rows and/or columns will be used.
Additional options planned:
- what to do when there are less values to completely fill the output CSV
(whether to always expand or expand when necessary, which is the default).
- what to do when there are more values (extend the table or ignore the extra
input values, which is the default).
_
add_args => {
# TODO
#fields => $App::CSVUtils::argspecopt_fields{fields}, # category:output
layout => {
summary => 'Specify how the output CSV is to be filled',
schema => ['str*', in=>[
'left_to_right_then_top_to_bottom',
'right_to_left_then_top_to_bottom',
'left_to_right_then_bottom_to_top',
'right_to_left_then_bottom_to_top',
'top_to_bottom_then_left_to_right',
'top_to_bottom_then_right_to_left',
'bottom_to_top_then_left_to_right',
'bottom_to_top_then_right_to_left',
]],
default => 'left_to_right_then_top_to_bottom',
tags => ['category:layout'],
},
filter => {
summary => 'Code to filter cells to fill',
schema => 'str*',
description => <<'_',
Code will be compiled in the `main` package.
Code is passed `($r, $output_row_num, $output_field_idx)` where `$r` is the
stash, `$output_row_num` is a 1-based integer (first data row means 1), and
`$output_field_idx` is the 0-based field index (0 means the first index). Code
is expected to return a boolean value, where true meaning the cell should be
filied.
_
tags => ['category:filtering'],
},
num_rows => {
summary => 'Number of rows of the output CSV',
schema => 'posint*',
req => 1,
tags => ['category:output'],
},
num_fields => {
summary => 'Number of fields of the output CSV',
schema => 'posint*',
req => 1,
tags => ['category:output'],
},
},
tags => ['category:generating', 'accepts-code'],
examples => [
{
summary => 'Fill number 1..100 into a 10x10 grid',
lib/App/CSVUtils/csv_fill_cells.pm view on Meta::CPAN
This utility takes values (from cells of a 1-column input CSV), creates an
output CSV of specified size, and fills the output CSV in one of several
possible ways ("layouts"): left-to-right first then top-to-bottom, or
bottom-to-top then left-to-right, etc.
Some illustration of the layout:
% cat 1-to-100.csv
num
1
2
3
...
100
% csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 ; # default layout is 'left_to_right_then_top_to_bottom'
field0,field1,field2,field3,field4,field5,field6,field7,field8,field9
1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21,22,23,24,25,26,27,28,29,30
...
91,92,93,94,95,96,97,98,99,100
% csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout top_to_bottom_then_left_to_right
field0,field1,field2,field3,field4,field5,field6,field7,field8,field9
1,11,21,31,41,51,61,71,81,91
2,12,22,32,42,52,62,72,82,92
3,13,23,33,43,53,63,73,83,93
...
10,20,30,40,50,60,70,80,90,100
% csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout top_to_bottom_then_right_to_left
91,81,71,61,51,41,31,21,11,1
92,82,72,62,52,42,32,22,12,2
93,83,73,63,53,43,33,23,13,3
...
100,90,80,70,60,50,40,30,20,10
% csv-fill-cells 1-to-100.csv --num-rows 10 --num-fields 10 --layout right_to_left_then_top_to_bottom
10,9,8,7,6,5,4,3,2,1
20,19,18,17,16,15,14,13,12,11
30,29,28,27,26,25,24,23,22,21
...
100,99,98,97,96,95,94,93,92,91
Some additional options are available, e.g.: a filter to let skip filling some
cells.
When there are more input values than can be fitted, the extra input values are
not placed into the output CSV.
When there are less input values to fill the specified number of rows, then only
the required number of rows and/or columns will be used.
Additional options planned:
=over
=item * what to do when there are less values to completely fill the output CSV
(whether to always expand or expand when necessary, which is the default).
=item * what to do when there are more values (extend the table or ignore the extra
input values, which is the default).
=back
This function is not exported.
Arguments ('*' denotes required arguments):
=over 4
=item * B<filter> => I<str>
Code to filter cells to fill.
Code will be compiled in the C<main> package.
Code is passed C<($r, $output_row_num, $output_field_idx)> where C<$r> is the
stash, C<$output_row_num> is a 1-based integer (first data row means 1), and
C<$output_field_idx> is the 0-based field index (0 means the first index). Code
is expected to return a boolean value, where true meaning the cell should be
filied.
=item * B<inplace> => I<true>
Output to the same file as input.
Normally, you output to a different file than input. If you try to output to the
same file (C<-o INPUT.csv -O>) you will clobber the input file; thus the utility
prevents you from doing it. However, with this C<--inplace> option, you can
output to the same file. Like perl's C<-i> option, this will first output to a
temporary file in the same directory as the input file then rename to the final
file at the end. You cannot specify output file (C<-o>) when using this option,
but you can specify backup extension with C<-b> option.
Some caveats:
=over
=item * if input file is a symbolic link, it will be replaced with a regular file;
=item * renaming (implemented using C<rename()>) can fail if input filename is too long;
=item * value specified in C<-b> is currently not checked for acceptable characters;
=item * things can also fail if permissions are restrictive;
=back
=item * B<inplace_backup_ext> => I<str> (default: "")
Extension to add for backup of input file.
In inplace mode (C<--inplace>), if this option is set to a non-empty string, will
rename the input file using this extension as a backup. The old existing backup
will be overwritten, if any.
=item * B<input_escape_char> => I<str>
( run in 2.823 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )