| 1 | package App::CSVUtils::csv_sum; | ||||||||||
| 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 | 2 | ); | |||||||||
| 15 | |||||||||||
| 16 | gen_csv_util( | ||||||||||
| 17 | name => 'csv_sum', | ||||||||||
| 18 | summary => 'Output a summary row which are arithmetic sum of data rows', | ||||||||||
| 19 | description => <<'MARKDOWN', | ||||||||||
| 20 | |||||||||||
| 21 | Non-numbers will be assumed to be 0. | ||||||||||
| 22 | |||||||||||
| 23 | Example: | ||||||||||
| 24 | |||||||||||
| 25 | # students.csv | ||||||||||
| 26 | name,score | ||||||||||
| 27 | andi,130 | ||||||||||
| 28 | budi,120 | ||||||||||
| 29 | chandra,120 | ||||||||||
| 30 | dudi,120 | ||||||||||
| 31 | |||||||||||
| 32 | % csv-sum students.csv | ||||||||||
| 33 | name,score | ||||||||||
| 34 | 0,490 | ||||||||||
| 35 | |||||||||||
| 36 | % csv-sum students.csv --with-data-row | ||||||||||
| 37 | name,score | ||||||||||
| 38 | andi,130 | ||||||||||
| 39 | budi,120 | ||||||||||
| 40 | chandra,120 | ||||||||||
| 41 | dudi,120 | ||||||||||
| 42 | 0,12.25 | ||||||||||
| 43 | |||||||||||
| 44 | MARKDOWN | ||||||||||
| 45 | add_args => { | ||||||||||
| 46 | %App::CSVUtils::argspecopt_with_data_rows, | ||||||||||
| 47 | }, | ||||||||||
| 48 | |||||||||||
| 49 | tags => ['category:summarizing', 'adds-field'], | ||||||||||
| 50 | |||||||||||
| 51 | 3 | ▶ | on_input_header_row => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 3 | my $r = shift; | |||||||||
| 53 | |||||||||||
| 54 | # we add this key to the stash | ||||||||||
| 55 | 3 | $r->{summary_row} = [map {0} @{$r->{input_fields}}]; | |||||||||
| 56 | 3 | $r->{row_count} = 0; | |||||||||
| 57 | |||||||||||
| 58 | # because input_* will be cleared by the time of after_read_input, | ||||||||||
| 59 | # we save and set it now | ||||||||||
| 60 | 3 | $r->{output_fields} = $r->{input_fields}; | |||||||||
| 61 | }, | ||||||||||
| 62 | |||||||||||
| 63 | 4 | ▶ | on_input_data_row => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 64 | 4 | my $r = shift; | |||||||||
| 65 | |||||||||||
| 66 | 4 | for my $j (0 .. $#{ $r->{input_fields} }) { | |||||||||
| 67 | 2 | ▶ | no warnings 'numeric', 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 12 | $r->{summary_row}[$j] += $r->{input_row}[$j]+0; | |||||||||
| 69 | } | ||||||||||
| 70 | 4 | ▶ | $r->{code_print_row}->($r->{input_row}) if $r->{util_args}{with_data_rows}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 4 | $r->{row_count}++; | |||||||||
| 72 | }, | ||||||||||
| 73 | |||||||||||
| 74 | 3 | ▶ | after_read_input => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 75 | 3 | my $r = shift; | |||||||||
| 76 | |||||||||||
| 77 | 3 | $r->{code_print_row}->($r->{summary_row}); | |||||||||
| 78 | }, | ||||||||||
| 79 | 2 | ); | |||||||||
| 80 | |||||||||||
| 81 | 2 | 1; | |||||||||
| 82 | # ABSTRACT: Output a summary row which are arithmetic sum of data rows | ||||||||||
| 83 | |||||||||||
| 84 | __END__ | ||||||||||