Title: Count how often a certain element occurs in an array
    Does Christian and christian and CHristian equate to the same? I am assuming yes. 
 
    One way:
 
open(FILENAMES,"<$filenames") || die "uanble to open file $filenames: $!";
my %MyNames = ();
my $in = 0;
while ( <FILENAMES> ) {
    chomp;
    $in++;
    next if ( /^\s*$/ ); # throw away blank lines
    #
    # If dont care about capitalization
    #
    $MyNames{lc($_)}++;
    #
    # if you care how capitalized then replace lc($_) with $_
    #
 }
 
my $MyTotal = 0;
#
# I am just print to screen, but could add file here with print.
#
foreach my $MyKey (sort keys %MyNames) {
    printf "%-25s  %6d\n", $MyKey, $MyNames{$MyKey};
    $MyTotal += $MyNames{$MyKey};
 }
printf "Rcds Read: %7d  Names: %7d\n", $in, $MyTotal;
 
Wags ;)
-----Original Message----- 
From: Heiko Nieke [mailto:[EMAIL PROTECTED]]
Sent: Thursday, June 06, 2002 08:03
To: [EMAIL PROTECTED]
Subject: Count how often a certain element occurs in an array

Hi,

It might be a silly question, but I am new to perl and I need to solve the following problem

- I have a list with approx. 400000 names

for example:
Christian
Sally
Christian
Klaus
....

Now I need to know how often each name appears in the list. Could anybody help me doing this with a perl script?

Thanks
Heiko

Reply via email to