"R. Joseph Newton" wrote:
>
> [EMAIL PROTECTED] wrote:
>
> > Hi! Maybe this is really easy, but hey, I'm a beginner with Perl.
> >
> > I'm trying to count the number of times a string is contained inside
> > another string. Here's a sample of my data, a set of colon-delimited
> > values from the Perl Soundex function, with a few pure numbers or letters
> > mixed in.
> >
> > A sample candidate string: :B000:W000:M260:8:
> > For each colon-delimited substring, such as :M260:, I want a count of how
> > many times it's found in each target string.
>
> It sounds like you need a count hash. You might try something like:
>
> my @tokens = split /:/;
> foreach (@tokens) {
> if ($tokenCount{$_}) {
> $tokenCount{$_}++;
> } else {
> $tokenCount{$_} = 1;
> }
> }
You don't need the "else" clause because perl will do the Right Thing on
auto-increment and you don't need the array @tokens because you are not
modifying $_ in the foreach loop.
foreach ( split /:/ ) {
$tokenCount{$_}++;
}
Or:
$tokenCount{$_}++ for split /:/;
However, looking at the data would imply that some of the results from
split/:/ would be an empty string '' which you don't want to include in
the hash.
$tokenCount{$_}++ for grep length, split /:/;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]