App-freqtable

 view release on metacpan or  search on metacpan

script/freqtable  view on Meta::CPAN

            unless ($Opts{percent} && $Opts{percent} < 2) {
                printf "%9d ", $n;
            }
            if ($Opts{percent}) {
                my $pct = $totoccurrences == 0 ? 0 : $n/$totoccurrences*100;
                printf "%6.2f%% ", $pct;
            }
        }
        print $k, "\n";
    }
}

sub _display_table {
    my @keys = keys %Occurences;

    my $totoccurrences = 0;
    $totoccurrences += $Occurences{$_} for @keys;

    if (defined $Opts{sort_sub}) {
        require Sort::Sub;
        my $sorter = Sort::Sub::get_sorter($Opts{sort_sub}, $Opts{sort_args});
        @keys = sort $sorter @keys;
    } else {
        @keys = sort {
        $Occurences{$b} <=> $Occurences{$a} ||
            ($Numeric ? $a <=> $b : $a cmp $b)
        } @keys;
    }

    print "\033[2J" if $Opts{clear_before_output};

    my $i = 0;
    for my $k (@keys) {
        $i++;
        my $n = $Occurences{$k};
        next if defined $Opts{min_rank} && $i < $Opts{min_rank};
        next if defined $Opts{max_rank} && $i > $Opts{max_rank};
        _print_freqline($n, $k, $totoccurrences);
    }

    _print_freqline($totoccurrences, "TOTAL", $totoccurrences) if $Opts{print_total};
}

sub run {
    $|++ if $Opts{output_every};

    my $i = 0;
    if ($Opts{mode} eq 'byte' || $Opts{mode} eq 'char') {
        @ARGV = (\*STDIN) unless @ARGV;
        for my $fn (@ARGV) {
            my $fh;
            if (ref $fn) {
                $fh = $fn;
            } else {
                open $fh, "<", $fn or do {
                    warn "freqtable: Can't open '$fn': $!\n";
                    next;
                };
            }
            if ($Opts{mode} eq 'byte') {
                binmode $fh;
            } else {
                binmode $fh, ":encoding(utf8)";
            }
            while (1) {
                read $fh, my $block, 4096;
                last if !length $block;
                for (split //, $block) {
                    $i++;
                    if ($Opts{ignore_case}) {
                        $Occurences{lc $_}++;
                    } else {
                        $Occurences{$_}++;
                    }
                    _display_table() if $Opts{output_every} && $i % $Opts{output_every} == 0;
                }
            }
        } # fn
    } elsif ($Opts{mode} eq 'word') {
        while (defined(my $line = <>)) {
            chomp $line;
            while ($line =~ /(\w+)/g) {
                $i++;
                if ($Opts{ignore_case}) {
                    $Occurences{lc $1}++;
                } else {
                    $Occurences{$1}++;
                }
                _display_table() if $Opts{output_every} && $i % $Opts{output_every} == 0;
            }
        }
    } elsif ($Opts{mode} eq 'line') {
        while (defined(my $line = <>)) {
            $i++;
            chomp $line;
            if ($Opts{ignore_case}) {
                $Occurences{lc $line}++;
            } else {
                $Occurences{$line}++;
            }
            _display_table() if $Opts{output_every} && $i % $Opts{output_every} == 0;
        }
    } elsif ($Opts{mode} eq 'number' || $Opts{mode} eq 'integer') {
        $Numeric++;
        while (defined(my $line = <>)) {
            $i++;
            my $num = $Opts{mode} eq 'integer' ? int($line) : $line + 0;
            $Occurences{$num}++;
            _display_table() if $Opts{output_every} && $i % $Opts{output_every} == 0;
        }
    } else {
        die "freqtable: BUG: Unknown mode '$Opts{mode}'";
    }

    _display_table();
}

# MAIN

parse_cmdline();
run();

1;



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