App-freqtable

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.008   2023-12-28  Released-By: PERLANCAR; Urgency: medium

        - Add option: --percent (-p).


0.007   2022-05-20  Released-By: PERLANCAR; Urgency: medium

        - [ux] Add -a as shortcut for --sort=asciibetically.

        - [doc] Forgot to document --sort-sub, --sort-arg, and -a.


0.006   2022-05-20  Released-By: PERLANCAR; Urgency: medium

script/freqtable  view on Meta::CPAN

my $tmp_freq;
my %Opts = (
    mode => 'line',
    ignore_case => 0,
    print_freq => 1,
    # XXX options to limit memory usage, e.g. max keys, max line length, --md5 (like in nauniq), ...
    min_freq => undef,
    max_freq => undef,
    sort_args => {},
    sort_sub => undef,
    percent => 0,
);

sub parse_cmdline {
    my $res = GetOptions(
        'bytes|c'   => sub { $Opts{mode} = 'byte' },
        'chars|m'   => sub { $Opts{mode} = 'char' },
        'words|w'   => sub { $Opts{mode} = 'word' },
        'lines|l'   => sub { $Opts{mode} = 'line' },
        'number|n'  => sub { $Opts{mode} = 'number' },
        'integer|i' => sub { $Opts{mode} = 'integer' },
        'ignore-case|f' => \$Opts{ignore_case},
        'no-print-freq|F' => sub { $Opts{print_freq} = 0 },
        'print-freq'      => sub { $Opts{print_freq} = 1 },
        'freq=s'    => \$tmp_freq,
        'sort-sub=s' => \$Opts{sort_sub},
        'sort-arg=s%' => $Opts{sort_args},
        'a'   => sub { $Opts{sort_sub} = 'asciibetically' },
        'percent|p' => sub { $Opts{percent} = 1 },
        'help|h'  => sub {
            print <<USAGE;
Usage:
  freqtable [OPTIONS]... < INPUT
  freqtable --help (or -h)
Options:
  --bytes, -c
  --chars, -m
  --words, -w
  --lines, -l
  --number, -n
  --integer, -i
  --ignore-case, -f
  --no-print-freq, -F
  --freq N|M-N|M-|-N
  --sort-sub=SPEC
  --sort-arg=ARG=VAL
  -a
  --percent, -p
For more details, see the manpage/documentation.
USAGE
            exit 0;
        },
    );

    if (defined $tmp_freq) {
        if ($tmp_freq =~ /\A\d+\z/) {
            $Opts{min_freq} = $Opts{max_freq} = $tmp_freq;
        } elsif ($tmp_freq =~ /\A-(\d+)\z/) {

script/freqtable  view on Meta::CPAN

        } @keys;
    }

    my $sum;

    for my $k (@keys) {
        my $n = $occurences{$k};
        next if defined $Opts{min_freq} && $n < $Opts{min_freq};
        next if defined $Opts{max_freq} && $n > $Opts{max_freq};
        if ($Opts{print_freq}) {
            if ($Opts{percent}) {
                $n = $totoccurrences == 0 ? 0 : $n/$totoccurrences*100;
                unless (defined $fmt) {
                    $fmt = "%5.2s%%\t%s\n";
                }
            } else {
                unless (defined $fmt) {
                    $fmt = "%" . ceil(log($n)/log(10)) . "s\t%s\n";
                }
            }
            printf $fmt, $n, $k;

script/freqtable  view on Meta::CPAN


=item * --sort-arg=ARGNAME=ARGVALUE

Pass argument(s) to the sort subroutine. Can be specified multiple times, once
for every argument.

=item * -a

Shortcut for C<--sort=asciibetically>.

=item * --percent, -p

Show frequencies as percentages.

=back

=head1 FAQ

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-freqtable>.

=head1 SOURCE



( run in 0.440 second using v1.01-cache-2.11-cpan-709fd43a63f )