![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Location: Olympia WA USA
Posts: 11
Rep Power: 0
![]() |
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.txtwill 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 ... |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3
![]() |
You might post the other code that deals with the $count var. The example you posted looks correct.
__________________
Neeley.org |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
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: 3As 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." |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2006
Location: Olympia WA USA
Posts: 11
Rep Power: 0
![]() |
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 ... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 9:44 PM |
| Array issues :( | Alo Tsum | Java | 10 | Nov 26th, 2005 6:45 PM |
| A standards question, optional inputs into Methods | Arla | C# | 4 | Apr 27th, 2005 11:38 PM |
| replace space with ' * ' | TecBrain | C++ | 15 | Apr 13th, 2005 1:32 PM |
| function | solomon_13000 | Java | 6 | Apr 3rd, 2005 12:42 AM |