#!/usr/bin/perl

use strict;
use warnings;

# PODNAME: count
# ABSTRACT: Counting utility for a file consisting of the fixed number of fields like CSV
our $VERSION = 'v0.0.2'; # VERSION

use App::count;

App::count->run(@ARGV);

__END__

=pod

=head1 NAME

count - Counting utility for a file consisting of the fixed number of fields like CSV

=head1 VERSION

version v0.0.2

=head1 SYNOPSIS

count C<-h>

count C<--help>

count [C<-g>|C<--group> E<lt>columnsE<gt>] [C<-c>|C<--count>] [C<-s>|C<--sum> E<lt>columnsE<gt>] [C<--min> E<lt>columnsE<gt>] [C<--max> E<lt>columnsE<gt>] [C<--avg>|C<--ave> E<lt>columnsE<gt>] [C<-m>|C<--map> E<lt>mapE<gt>] [C<-M>|C<--map-file> E<lt>filenameE<gt>] [C<-t>|C<--delimiter> E<lt>delimiterE<gt>] C<files>...

  # show brief instruction
  count -h

  # show POD
  count --help

  # count the number of records grouping by the column 1 and 2
  # The column number is 1-origin
  count -g 1,2 file

  # count the sum of the column 3 grouping by the column 1 and 2
  # field delimiter is ','
  count -g 1 -g 2 -s 3 -t ',' file

  # Ouput min,max,average of the column 2 and the column 3 grouping by the column 1
  count -g 1 --min 2 --max 2 --avg 2 --min 3 --max 3 --avg 3

=head1 DESCRIPTION

I has written a oneliner like the following repeatedly and repeatedly, to make some statistics.

  perl -e 'while(<>) { @t = split /\t/; ++$c{$t[0]}; } foreach my $k (keys %c) { print "$k,$c{$k}\n" }'

Yes, we can write as the following making use of command line option.

  perl -an -F "\t" -e '++$c{$F[0]} END { foreach my $k (keys %c) { print "$k,$c{$k}\n" }'

This is still verbose in contrast with doing. By this script, you can write as the following. Please NOTE that the number is 1-origin.

  count -g 1 -t "\t"

Conforming to Unix philosophy, this scirpt does NOT have configurable sort functionality.
If you want it, you can use C<sort> command.

  count -g 1 -t "\t" | sort -k n1

=head1 OPTIONS

=head2 C<-h>

Show brief instruction.

=head2 C<--help>

Show this POD.

=head2 C<-g>|C<--group> E<lt>columnsE<gt>

Specify group columns like GROUP BY in SQL.
You can specify multiple times and/or as comma separated numbers.

=head2 C<-c>|C<--count>

Output the number of records. If no other output option is specified, process as if this option is specified.

=head2 C<-s>|C<--sum> E<lt>columnsE<gt>

Output the sum of the specified column.
You can specify multiple times and/or as comma separated numbers.

=head2 C<--min> E<lt>columnsE<gt>

Output the minimum value of the specified column.
You can specify multiple times and/or as comma separated numbers.

=head2 C<--max> E<lt>columnsE<gt>

Output the maximum value of the specified column.
You can specify multiple times and/or as comma separated numbers.

=head2 C<--avg>|C<--ave> E<lt>columnsE<gt>

Output the average of the specified column.
You can specify multiple times and/or as comma separated numbers.

=head2 C<-m>|C<--map> E<lt>mapE<gt>

Output mapped value of the specified column by the specified mapping key.
Argument is a list of key and column like.

  -m 0,class,1,subclass

=head2 C<-M>|C<--map-file> E<lt>filenameE<gt>

Specify map file used by -m option. The map file is YAML file having the following structure.

  <key1>:
    <number11>: <value11>
    <number12>: <value12>
  <key2>:
    <number21>: <value21>
    <number22>: <value22>

=head2 C<-t>|C<--delimiter> E<lt>delimiterE<gt>

Specify field separator character. The character is used by both of input and output.

=head2 C<files>...

Input files. If no files are specified, read from STDIN.

=head1 AUTHOR

Yasutaka ATARASHI <yakex@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Yasutaka ATARASHI.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
