Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: compare two strings and return only he unique values.

by smls (Friar)
on Feb 03, 2013 at 21:27 UTC ( #1016862=note: print w/ replies, xml ) Need Help??


in reply to compare two strings and return only he unique values.

Note:

The various solutions posted above don't behave exactly the same. This is because the OP hasn't described the problem very precisely, so the commenters (myself included) have made implicit assumptions about the intended meaning of "unique values from two strings".

Interpreting the term "values" in that quote as characters, there are at least two different meanings:

  • Characters that appear exactly once in one of the strings, and don't appear in the other.

    This is what the above solutions by blue_cowdawg and Not_a_Number do.

  • Characters that appear one or more times in one of the strings, and don't appear in the other.

    This is what the above solutions by tobyink and myself do.

If "values" is supposed to mean something other than individual characters, simply adapt any of the above solutions by replacing the split(//, ...) calls with a statement that tokenizes the string into a list of values in the intended way.

In case the strings are not intended to be tokenized in a fixed way, but instead what you are looking for is arbitrary text fragments that appear in one of the strings, but don't appear in that same spot in the other, look into Algorithm::Diff


Comment on Re: compare two strings and return only he unique values.
Download Code
Re^2: compare two strings and return only he unique values.
by Kenosis (Priest) on Feb 03, 2013 at 21:42 UTC

    Excellent points++! It's evident, however, that the OP's considering the possibility of characters repeating in strings, as shown in the initial posting:

    my ($str1, $str2) = ("ABCDE", "BBBBB");
      *** Please delete. Misplaced dupe. ***

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016862]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-08-30 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls