Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
more useful options
 
PerlMonks  

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

by blue_cowdawg (Prior)
on Feb 04, 2013 at 15:11 UTC ( #1016957=note: print w/ replies, xml ) Need Help??


in reply to Re^2: compare two strings and return only he unique values.
in thread compare two strings and return only he unique values.

      Won't show "a" when:

That's because "a" isn't unique. :)

printf "%s\n",join(",", keys %een);
would show "a" and all the other letters once.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg


Comment on Re^3: compare two strings and return only he unique values.
Download Code
Re^4: compare two strings and return only he unique values.
by Kenosis (Priest) on Feb 04, 2013 at 20:36 UTC

    Your code quite effectively forms a union of the two strings' characters. However, the OP's request was to 1) compare two strings, and 2) return only the unique values. Where does your code compare the two strings? Forming a union of their characters does not compare the two strings. In fact, if we strictly regard their union as a non-multiset of characters, "a" is unique, since (set notation): {a} = {a,a} = {a,a,a}. Thus, "a" should still appear in their union--even with two occurrences within one string.

    As has been point out, the OP's specs are equivocal, yet I tend to favor "unique" as meaning a character (replicated or not) that only belongs to one or the other string (i.e., "unique to")--and is not in both.

          I am trying to display unique values from two strings "ABCDE", "BCDEO" to return "AO"

      was the OP's request. As a matter of fact my code does exactly what they were asking for. Splitting the two strings into single characters and counting the character's occurrence within the string is how I'd approach it.


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

        Yes--I do understand, and your code works perfectly well for that initial case. Then the OP shows the following case:

        my $string1="ABCDE"; my $string2="BBBBB";

        Had the OP used $string2="FFFFF";, your code would not show "F", even though--after a string comparison--"F" is unique to $string2.

        I see many good minds working on this issue with different POVs. Again, I think it evidences that the specs are equivocal.

        BTW - I didn't downvote yours or others' similar interpretation of "unique;" I merely commented on it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-04-21 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls