Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here's a bit-logic based approach:

#!/usr/bin/perl -w use strict; my %groups = ( group1 => ['cooling', 'rooting', 'hooting', 'looking', 'doormat', +'cooking', 'cookies', 'noodles'], # desired result "-oo----" group2 => ['yourself', 'southern'], # desired result "-ou--e--" group3 => ['arden', 'arlen', 'asked'] # desired result "a--e-" ); sub show_bits { #print unpack("B*", shift), "\n"; } for my $g (values %groups) { my $w1 = $g->[0]; my $and = "\xff" x length($w1); my $or = "\0" x length($w1); for my $w (@$g) { print "$w\n"; #print join(" " x 7, split //, $w), "\n"; show_bits($w); $and &= $w; show_bits($and); $or |= $w; show_bits($or); } my $xor = $and ^ $or; show_bits($xor); $xor =~ tr/\0/\xff/c; show_bits($xor); my $mask = ~$xor; show_bits($mask); my $common = $w1 & $mask; $common =~ tr/\0/-/; print "$common\n"; my $i = -1; print map {$i++; $_ ne "-" ? "$_ : $i\n" : ()} split //, $common; print "\n"; } __END__ yourself southern -ou--e-- o : 1 u : 2 e : 5 cooling rooting hooting looking doormat cooking cookies noodles -oo---- o : 1 o : 2 arden arlen asked a--e- a : 0 e : 3

Uncomment the commented out prints to see how the bit strings are being transformed/combined...

(Update: fixed cut-n-paste error $and ^= $or — thanks johngg)

In reply to Re: Matching or masking specific characters in an array of strings by almut
in thread Matching or masking specific characters in an array of strings by duggles

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2018-05-21 10:03 GMT
    Find Nodes?
      Voting Booth?