App-CSVUtils

 view release on metacpan or  search on metacpan

lib/App/CSVUtils/csv_fill_cells.pm  view on Meta::CPAN

 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>

Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS.

Defaults to C<\\> (backslash). Overrides C<--input-tsv> option.

=item * B<input_filename> => I<filename> (default: "-")

Input CSV file.

Use C<-> to read from stdin.

Encoding of input file is assumed to be UTF-8.

=item * B<input_header> => I<bool> (default: 1)

Specify whether input CSV has a header row.

By default, the first row of the input CSV will be assumed to contain field
names (and the second row contains the first data row). When you declare that
input CSV does not have header row (C<--no-input-header>), the first row of the
CSV is assumed to contain the first data row. Fields will be named C<field1>,
C<field2>, and so on.

=item * B<input_quote_char> => I<str>

Specify field quote character in input CSV, will be passed to Text::CSV_XS.

Defaults to C<"> (double quote). Overrides C<--input-tsv> option.

=item * B<input_sep_char> => I<str>

Specify field separator character in input CSV, will be passed to Text::CSV_XS.

Defaults to C<,> (comma). Overrides C<--input-tsv> option.

=item * B<input_skip_num_lines> => I<posint>

Number of lines to skip before header row.

This can be useful if you have a CSV files (usually some generated reports,
sometimes converted from spreadsheet) that have additional header lines or info
before the CSV header row.

See also the alternative option: C<--input-skip-until-pattern>.

=item * B<input_skip_until_pattern> => I<re_from_str>

Skip rows until the first header row matches a regex pattern.

This is an alternative to the C<--input-skip-num-lines> and can be useful if you
have a CSV files (usually some generated reports, sometimes converted from
spreadsheet) that have additional header lines or info before the CSV header
row.

With C<--input-skip-num-lines>, you skip a fixed number of lines. With this
option, rows will be skipped until the first field matches the specified regex
pattern.



( run in 0.883 second using v1.01-cache-2.11-cpan-39bf76dae61 )