Rob, thanks!  This might work, but can you tell me how to make the pattern
(:8: in your example) a variable?  And I think I can count the number of
elements in the @y array and use that as my match count, too.

Thanks,

Scott

Scott E. Robinson
SWAT Team
UTC Onsite User Support
RR-690 -- 281-654-5169
EMB-2813N -- 713-656-3629


                                                                                       
                                     
                      "Hanson, Rob"                                                    
                                     
                      <[EMAIL PROTECTED]       To:       "'[EMAIL PROTECTED]'"         
                       
                      .com>                      <[EMAIL PROTECTED]>, [EMAIL 
PROTECTED]                      
                                               cc:                                     
                                     
                                               Subject:  RE: COunting the number of 
times a string matches in another stri  
                      03/12/03 02:25 PM          ng                                    
                                     
                                                                                       
                                     
                                                                                       
                                     



You might be able to shorten this, but this seems to work ok...

$x = ':B000:W000:M260:8:';
@y = $x =~ /:8:/g;
print scalar(@y);

@y will contain the matches, one for each element of the array.  Grabbing
the array in scalar context will get you the count of the matches.
...There
is probably a faster way though, one that won't require building the @y
array.

Rob



-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
Sent: Wednesday, March 12, 2003 2:58 PM
To: [EMAIL PROTECTED]
Subject: COunting the number of times a string matches in another string


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.

Some sample target strings:

:L520:T400:C000:S000:L200:8:          <-bare numbers are possible, like
this :8:
:L520:T400:C000:S000:L200:8:
:L520:T400:C000:S000:L200:24:E214:
:L520:T400:C000:S000:M:24:E214:       <-note the :M: string, just for
variety
:L520:T400:C000:S000:L200:14:E214:
:L520:T400:C000:S000:L200:14:E214:
:L520:M260:C000:S000:L200:14:E214:    <-this should match once
:L520:T400:M260:S000:M260:14:E214:    <-this should match twice
:L520:T400:C000:S000:L200:14:E214:

I tried the longhand approach using arrays to compare each colon-delimited
substring of the candidate string to all the substrings in the targets and
that's way too slow.  Since I only need to know how many times each string
is contained inside the target string, I thought substring matches might do
it in one operation -- but I don't know how to count the number of matches.
I hope somebody out there does -- or knows an even better way.

Thanks,

Scott

Scott E. Robinson
SWAT Team
UTC Onsite User Support
RR-690 -- 281-654-5169
EMB-2813N -- 713-656-3629


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to