#!/usr/bin/perl -w
# This is a simple script to count the number of distinct entries sent it.

use strict;

my $VERSION=0.1;
my %hash = ();

while (<>) {
    chomp;
    if ( exists $hash{$_} ){
        $hash{$_} = $hash{$_} + 1;
    } else {
        $hash{$_} = 1;
    };
    if ( scalar keys ( %hash ) > 5000000 ){
        die "The number of distinct entries is too large.";
    }
}
print STDERR "distinct-keys, occurances\n";
for my $key ( sort keys %hash ){
    print STDOUT "$key,$hash{$key}\n";
}

=head1 NAME

distinct

=head1 DESCRIPTION

Prints to STDOUT a sorted list of items followed by a comma then followed
by the number of instances of that item. In fairness it is only a wrapped
implementation of a counter using a simple hash.

=head1 README

Prints to STDOUT a sorted list of items followed by a comma then followed
by the number of instances of that item. In fairness it is only a wrapped
implementation of a counter using a simple hash.

Take this file C<example.txt> as an example:

=over 8

=item C<a>

=item C<b>

=item C<c>

=item C<a>

=item C<b>

=item C<a>

=back

If you were to C<cat example | distinct> you should see:

=over 8

=item C<distinct-keys, occurances>

=item C<a,3>

=item C<b,2>

=item C<c,1>

=back

=head1 PREREQUISITES

This script requires the C<strict> module.

=head1 COREQUISITES

none

=pod OSNAMES

any

=pod SCRIPT CATEGORIES

UNIX/System_administration

=cut