I still don't really understand what you want from the OPed code, but here's an approach that produces the same result and which may (or may not) be considered "neater." I've made no attempt to Benchmark this code. Note that the strings need not all be the same length. Also, a  '0' character (which tests false) is handled properly.

c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 'TTT0TTTTT'; my $y = 'TTTTTTTT'; my $z = 'TBTTTTT'; ;; print qq{only one of '$_'} for one_only($x, $y, $z); ;; sub one_only { my %freq; return grep $freq{$_} == 1, map ++$freq{$_} && $_, map split('', $_), @_ ; } " only one of '0' only one of 'B'

Update: Actually, here's an equivalent variation based on substr that might actually be significantly faster than split-ing every string into countless characters. (Again, I haven't Benchmark-ed.)

c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; for my $ar_vector ( [ [], ], [ [], '' ], [ [], '', '' ], [ [], '', '', '' ], [ [ 'A' ], 'A' ], [ [], qw(A A) ], [ [], qw(AA) ], [ [], qw(A A +A) ], [ [ qw(A B) ], qw(A B) ], [ [ qw(A B C) ], qw(A B C) ], [ [ qw(A B C D E F G H) ], 'HGFE', 'ABCD' ], [ [ qw(A B C D E F G H I) ], 'GFED', 'HI', 'ABC' ], [ [], 'TTTTTTT', 'TTTTTTTTT' ], [ [ 'A' ], 'TTTATTTT', 'TTTTTTT' ], [ [ qw(A B) ], 'TBTTTTT', 'TTTATTT' ], [ [ qw(A B) ], 'TTTTTTA', 'TTTTTTB' ], ) { my ($ar_expected_singletons, @strings) = @$ar_vector; ;; my $strings_cmnt = qq{@{[ map qq{'$_'}, @strings ]}}; ;; my @got_singletons = singletons(@strings); is_deeply \@got_singletons, $ar_expected_singletons, qq{($strings_cmnt) singleton(s): (@$ar_expected_singletons)}; } ;; done_testing; ;; exit; ;; sub singletons { my %freq; for (@_) { for my $o (0 .. length($_)-1) { ++$freq{substr $_, $o, 1} } }; return sort grep $freq{$_} == 1, keys %freq; } " ok 1 - () singleton(s): () ok 2 - ('') singleton(s): () ok 3 - ('' '') singleton(s): () ok 4 - ('' '' '') singleton(s): () ok 5 - ('A') singleton(s): (A) ok 6 - ('A' 'A') singleton(s): () ok 7 - ('AA') singleton(s): () ok 8 - ('A' 'A' 'A') singleton(s): () ok 9 - ('A' 'B') singleton(s): (A B) ok 10 - ('A' 'B' 'C') singleton(s): (A B C) ok 11 - ('HGFE' 'ABCD') singleton(s): (A B C D E F G H) ok 12 - ('GFED' 'HI' 'ABC') singleton(s): (A B C D E F G H I) ok 13 - ('TTTTTTT' 'TTTTTTTTT') singleton(s): () ok 14 - ('TTTATTTT' 'TTTTTTT') singleton(s): (A) ok 15 - ('TBTTTTT' 'TTTATTT') singleton(s): (A B) ok 16 - ('TTTTTTA' 'TTTTTTB') singleton(s): (A B) 1..16 ok 17 - no warnings 1..17


Give a man a fish:  <%-{-{-{-<


In reply to Re: compare initial (updated) by AnomalousMonk
in thread compare initial by dideod.yang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.