in reply to Mostly Harmless
G'day emdub,
Welcome to the monastery.
The following code shows how you could handle multiple pairs of strings. I've hard-coded a space to match your "Output". You haven't indicated exactly what you mean by "letter": I've used any character matching /\p{Alpha}/ (see "perluniprops: Properties accessible through \p{} and \P{}") — your concept of "letter" may differ.
#!/usr/bin/env perl -l use strict; use warnings; my @pairs = (['CHARLIE ROOT', 'HARRY NODE'], ['abc1', 'cde1']); print get_non_shared(reverse @$_), ' ', get_non_shared(@$_) for @pairs +; sub get_non_shared { my ($ref, $src) = @_; my %ref_char = map { $_ => 1 } grep { /\p{Alpha}/ } split '' => $r +ef; join '' => grep { ! $ref_char{$_} } split '' => $src; }
Output:
CLI T Y ND ab1 de1
Note that you haven't indicated how you get your pairs of strings nor what combinations are involved in the operations. Does one string equate to one line from a file? Are you operating on just single pairs from matching lines (i.e. ~300 operations) or all possible combinations (i.e. ~90,000 operations)? Depending on the answers to those — and potentially other questions, such as how often do you plan to run the program — some efficiency optimisations may be useful.
-- Ken
|
|---|