Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Matching or masking specific characters in an array of strings

by almut (Canon)
on Dec 19, 2008 at 14:25 UTC ( [id://731551]=note: print w/replies, xml ) Need Help??


in reply to Matching or masking specific characters in an array of strings

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)

Replies are listed 'Best First'.
Re^2: Matching or masking specific characters in an array of strings
by duggles (Acolyte) on Dec 19, 2008 at 14:50 UTC

    Thank you very much almut! We can consider this question closed!

    I knew I could count on the monks! In addition to my regex skills being way below what they should be, my bit manipulation and use of packing is much worse...

    The code is elegant, effective, and I'm certain much faster than what I had. I will be studying this code to see if I can use this sort of logic to help me in future coding efforts.

    Thanks again!

    If the knowledge and innovative thinking of the perlmonks could only be harnessed for a single purpose - I'm sure world peace would be a piece of cake!!!!

    --- duggles ;-)

    Life is short, but it's wide -- Chuck Pyle
Re^2: Matching or masking specific characters in an array of strings
by nbezzala (Scribe) on Apr 12, 2012 at 16:07 UTC
    Can someone please explain what is happening here? I'm wondering why a solution like this isn't mentioned in this thread - $w =~ s/^aeiou/-/g;

      They're not talking about "masking away" characters from a given character set in a single string. They're talking about masking multiple strings together in order find out which characters those strings have in common. Hence:

      
        southern
      - yourself
      ----------
        -ou--e--
      
      

      (where - (minus) is the masking operation.)

      I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found