Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with specific search in string 1

Status
Not open for further replies.
Jan 21, 2004
12
CA
Hi All,

I'm having a problem with a regex to look for repeated characters in a string.

there is about 10 000 00 different strings and i need to know how to look for any character that appears 3 times in the string.

EX:

my $a = 'abcabca';

something like

if (char appears 3 times in $a)
{
# do something
}

wihout actually specifying which character to look for.

I hope im clear in what im looking for, if not, please let me know.

Thank you very much,

P.
 
One way is to split the string into characters and count how many of each one. Here's an example:
Code:
@strings = ("abcabc", "abcdabcd", "abcabcc", "abcabcb", "abc", "aaa", "abbccbcaaacc");
foreach $string (@strings) {
    @chars = split //, $string; # split into characters
    undef(%num);
    foreach $char (@chars) {
        $num{$char}++; # add to count of character
    }
    foreach $repeat (keys %num) {
        print "repeat '$repeat' in $string\n" if ($num{$repeat} >= 3);
    }
}
 
[raider2001] - i like that! good solution!!!


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top