View Single Post
Old Jul 13th, 2006, 9:46 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
This works for me:

#!/usr/bin/perl

my $data = "this_is_a_test.txt";

$count = ($data =~ tr/_/_/);

print("Count is: $count\n");


[ir@grendel]# ./cchar.pl
Count is: 3


As an alternative, I wrote a small function also...

#!/usr/bin/perl

# Count letter frequency.
# Infinite Recursion - 13 JUL 2006

use strict;

my $base = "this_is_a_test.txt";
my $count = CountUnderscore($base);

print "Number of _ is: $count\n";


sub CountUnderscore ($base)
{
    my @find = ('_');
    my %freq = (); 

    foreach my $findc (@find) 
    {   
        my $count = 0;
        foreach my $basec (split(//, $base)) 
        {       
            if ($basec eq $findc)  
            {           
                ++$count;
            }           
        }       

        $freq{$findc} = $count; 
    }   

    return $freq{"_"};
}



[ir@grendel]# ./cfreq.pl
Number of _ is: 3
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote