Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl Question

String matching like characters

         

Key_Master

1:28 am on Aug 27, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Any ideas on an easy way to match like characters within two separate strings. Most programming languages have a string matching command that allows for this and I'm hoping Perl does as well. This way I won't need to sort each string (Faster).

Example:

$a = "abcd";
$b = "defg";

I need to find a way to sort $a and $b, toss out "abc" and "efg", to make $c = "d".

Brett_Tabke

10:02 am on Aug 28, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You know - I looked at this and looked at this. If I knew more about what it was you were trying to do, I think I could hack something together. Basically, my approach would be to use the range option of a regex in a substitution:

s/[$a]//gi;
or
s/[$a]/[$b]/gi;

I know that isn't the code you are looking for, but that is the starting approach I'd use. The range option is excellent for things like balanced text.

sugarkane

8:05 pm on Aug 29, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




while ($b=~/[$a]/gi) {
$c.=$& unless ($c=~/$&/);
}

...basically pulls out a single copy of each character the two strings have in common and puts them in $c.

I'm not sure if this is what you're looking for though?

Key_Master

5:16 am on Aug 30, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys. sugarkane's suggestion might be what I'm lookung for. I give it a shot over the weekend and let you know how it turns out.

Bolotomus

6:39 pm on Sep 6, 2001 (gmt 0)

10+ Year Member



Most programming languages have a string matching command that allows for this and I'm hoping Perl does as well.

This statement leads me to believe that I don't understand the problem. I've used quite a few programming languages, and while they almost all have some kind of string search feature (like pos() to locate the position of a substring in a larger string) I've run into very few intersection routines.

What you are looking for is basically an intersection of two character sets, no? So if one string is "PUNCH JOKERS" and the other string is "PERL HACKER" then it finds all characters found in both strings, namely "PERHCKR" How are duplicate characters handled?

What is this about sorting? You mean sort the characters of the string, so that "EBACD" becomes "ABCDE"?

If this is what you want, it's not so hard, but this is certainly nothing that any programming language that I've ever heard of has "built-in."