| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2024-2025 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package App::csvtool::Summarizing 0.05; | ||||||||||
| 7 | |||||||||||
| 8 | 15 | ▶ | use v5.26; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 15 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 15 | ▶ | use experimental 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | C<App::csvtool::Summarizing> - summarize tabular data in F<csvtool> | ||||||||||
| 15 | |||||||||||
| 16 | =head1 COMMANDS | ||||||||||
| 17 | |||||||||||
| 18 | =for highlighter | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | |||||||||||
| 22 | package App::csvtool::count | ||||||||||
| 23 | 15 | { | |||||||||
| 24 | |||||||||||
| 25 | =head2 count | ||||||||||
| 26 | |||||||||||
| 27 | $ csvtool count -fFIELD INPUT... | ||||||||||
| 28 | |||||||||||
| 29 | Counts the number of rows that have distinct values for the selected field. | ||||||||||
| 30 | |||||||||||
| 31 | Outputs a new table having only two columns. The first column will be the | ||||||||||
| 32 | distinct values of the selected field that were found in the input, the second | ||||||||||
| 33 | column will be an integer giving the number of rows of the input which had | ||||||||||
| 34 | that that value. Rows are output in order of the first time each distinct | ||||||||||
| 35 | value was seen in the input. | ||||||||||
| 36 | |||||||||||
| 37 | Besides the selected key field, all other fields of the input are ignored. | ||||||||||
| 38 | |||||||||||
| 39 | =head3 --field, -f | ||||||||||
| 40 | |||||||||||
| 41 | The field index to use as the counting key (defaults to 1). | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | 15 | ▶ | use constant COMMAND_DESC => "Count the number of rows by the value in FIELD"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | 15 | ▶ | use constant COMMAND_OPTS => ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | { name => "field|f=", description => "Field to extract", | ||||||||||
| 49 | default => 1 }, | ||||||||||
| 50 | 15 | ); | |||||||||
| 51 | |||||||||||
| 52 | 15 | ▶ | use constant WANT_READER => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 15 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | |||||||||||
| 55 | 2 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||
|
Subroutine
run: called CC=5 SCAR=16.1
| |||||||||||
| 56 | { | ||||||||||
| 57 | 2 | my $FIELD = $opts->{field}; | |||||||||
| 58 | |||||||||||
| 59 | # 1-indexed | ||||||||||
| 60 | 2 | $FIELD--; | |||||||||
| 61 | |||||||||||
| 62 | 2 | my @keys; # maintain original first-seen order | |||||||||
| 63 | my %count_for_key; | ||||||||||
| 64 | |||||||||||
| 65 | 2 | ▶ | while( my $row = $reader->() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 10 | my $key = $row->[$FIELD]; | |||||||||
| 67 | 10 | ▶ | defined $key or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 10 | ▶ | exists $count_for_key{$key} or push @keys, $key; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 10 | $count_for_key{$key}++; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 2 | foreach my $key ( @keys ) { | |||||||||
| 74 | 8 | $output->( [ $key, $count_for_key{$key} ] ); | |||||||||
| 75 | } | ||||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | package App::csvtool::sum | ||||||||||
| 80 | 15 | { | |||||||||
| 81 | |||||||||||
| 82 | =head2 sum | ||||||||||
| 83 | |||||||||||
| 84 | $ csvtool sum -fFIELD INPUT... | ||||||||||
| 85 | |||||||||||
| 86 | Sums the values in a given column, outputting a total at the end. Optionally, | ||||||||||
| 87 | rows can be grouped together by one or more group-key fields, maintaining | ||||||||||
| 88 | separate totals per unique set of values in those fields. | ||||||||||
| 89 | |||||||||||
| 90 | Outputs a new table with a row per set of keys. Each row will contain all of | ||||||||||
| 91 | the key values, in the order given by the list of key fields, followed by its | ||||||||||
| 92 | group total. If there are no key fields the output will be a single row | ||||||||||
| 93 | containing just a single value. | ||||||||||
| 94 | |||||||||||
| 95 | =head3 --field, -f | ||||||||||
| 96 | |||||||||||
| 97 | The field index of the column to sum (defaults to 1). | ||||||||||
| 98 | |||||||||||
| 99 | =head3 --group, -g | ||||||||||
| 100 | |||||||||||
| 101 | Optional comma-separated list of additional fields to use as group keys. | ||||||||||
| 102 | Defaults to empty, meaning that no key-based grouping will occur. | ||||||||||
| 103 | |||||||||||
| 104 | =cut | ||||||||||
| 105 | |||||||||||
| 106 | 15 | ▶ | use constant COMMAND_DESC => "Sum the values in the FIELD column"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 107 | |||||||||||
| 108 | 15 | ▶ | use constant COMMAND_OPTS => ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 109 | { name => "field|f=", description => "Field to sum", | ||||||||||
| 110 | default => 1 }, | ||||||||||
| 111 | { name => "group|g=", description => "Field(s) to use as group keys", | ||||||||||
| 112 | default => "" }, | ||||||||||
| 113 | 15 | ); | |||||||||
| 114 | |||||||||||
| 115 | 15 | ▶ | use constant WANT_READER => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 116 | 15 | ▶ | use constant WANT_OUTPUT => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 117 | |||||||||||
| 118 | 2 | ▶ | sub run ( $pkg, $opts, $reader, $output ) | ||||||||
|
Subroutine
run: called CC=7 SCAR=19.5
| |||||||||||
| 119 | { | ||||||||||
| 120 | 2 | my $VALFIELD = $opts->{field}; | |||||||||
| 121 | 2 | my @KEYFIELDS = split m/,/, $opts->{group}; | |||||||||
| 122 | |||||||||||
| 123 | # 1-indexed | ||||||||||
| 124 | 2 | $VALFIELD--; | |||||||||
| 125 | 2 | $_-- for @KEYFIELDS; | |||||||||
| 126 | |||||||||||
| 127 | 2 | my @keysets; | |||||||||
| 128 | my %totals; | ||||||||||
| 129 | |||||||||||
| 130 | 2 | ▶ | while( my $row = $reader->() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 9 | my $key = join "\0", @{$row}[@KEYFIELDS]; | |||||||||
| 132 | 9 | my $value = $row->[$VALFIELD]; | |||||||||
| 133 | 9 | ▶ | defined $value or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | |||||||||||
| 135 | 9 | ▶ | exists $totals{$key} or push @keysets, $key; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 9 | $totals{$key} += $value; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 2 | foreach my $key ( @keysets ) { | |||||||||
| 140 | 3 | ▶ | my @keys = length $key ? ( split m/\0/, $key ) : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 3 | $output->( [ @keys, $totals{$key} ] ); | |||||||||
| 142 | } | ||||||||||
| 143 | } | ||||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | =head1 AUTHOR | ||||||||||
| 147 | |||||||||||
| 148 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | 15 | 0x55AA; | |||||||||