Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 12th, 2006, 9:53 AM   #1
tbohon
Newbie
 
Join Date: Mar 2006
Location: Olympia WA USA
Posts: 11
Rep Power: 0 tbohon is an unknown quantity at this point
Question Counting a single character in a string

I have a list of a directory and need to process the files depending on the number of underscore characters in the file name. For example, the files
filename_date.txt

and

file_name_date.txt
will be processed differently by the script.

I've tried

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

but it doesn't seem to work - I continue to get a zero (0) for $count.

Is there a flaw in my logic above? Is there a better way to do this?

Thanks in advance!

Tom
__________________
My mind is like a steel whatchamacallit ...
tbohon is offline   Reply With Quote
Old Jul 13th, 2006, 5:16 AM   #2
dr.p
Programmer
 
dr.p's Avatar
 
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3 dr.p is on a distinguished road
You might post the other code that deals with the $count var. The example you posted looks correct.
__________________
Neeley.org
dr.p is offline   Reply With Quote
Old Jul 13th, 2006, 8:46 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 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
Old Jul 13th, 2006, 9:23 AM   #4
tbohon
Newbie
 
Join Date: Mar 2006
Location: Olympia WA USA
Posts: 11
Rep Power: 0 tbohon is an unknown quantity at this point
Thanks to both of you. Turns out that the value I was moving to $data was wrong ... should have checked that before posting I guess ... just too many things happening at once and too many 'break/fixes' on my plate.

Appreciate it!

Best,

Tom
__________________
My mind is like a steel whatchamacallit ...
tbohon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 5:45 PM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 10:38 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 12:32 PM
function solomon_13000 Java 6 Apr 2nd, 2005 11:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC