Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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

by AnomalousMonk (Canon)
on Feb 04, 2013 at 01:57 UTC ( #1016886=note: print w/replies, xml ) Need Help??

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

Here's a substution-based version that may be somewhat along the lines Learnlot1 was thinking of originally. It does not generalize well to handling more than two strings as the split-to-hash approaches do. It suffers from the vagueness of the implied specification as other approaches do. But it works (for some definition of 'works').

>perl -wMstrict -le "for my $ar_vector ( [ qw(ABCDE BCDEO) ], [ qw(ABCDE BBBBB) ], [ qw(ABCDEA BCDEO) ], ) { my ($s1, $s2) = @$ar_vector; printf qq{'$s1' '$s2' -> '%s' \n}, non_common($s1, $s2); } ;; sub non_common { my ($sa, $sb) = @_; ;; (my $a_sans_b = $sa) =~ s{ [\Q$sb\E]+ }{}xmsg; (my $b_sans_a = $sb) =~ s{ [\Q$sa\E]+ }{}xmsg; return $a_sans_b . $b_sans_a; } " 'ABCDE' 'BCDEO' -> 'AO' 'ABCDE' 'BBBBB' -> 'ACDE' 'ABCDEA' 'BCDEO' -> 'AAO'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016886]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-02-25 19:06 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (367 votes). Check out past polls.