Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found