Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by AnomalousMonk (Abbot)
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'


Comment on Re: compare two strings and return only he unique values.
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2015-07-28 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls