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

Now, I realise that my post is not going to make any ground-breaking contribution anymore this late in the process ... and after the neat bit-wise operation solution suggested by almut there is little to be added. Still, I would like to assert that this is a neat problem and offer my humble solution which may at least win a prize for conciseness (and if not - might at least get considered in the Perl obfuscation section ;-). The noteworthy feature about this implementation is that it accomplishes the entire analysis process in a single regex!

Here it goes:
#! /usr/bin/perl/ -w use strict; my @strings = qw ( cooling rooting hooting looking doormat ); our @common_letters; my $reference = shift @strings; () = $reference =~ /(.)(?{ my $letter = $1; my $position = $-[0]; my $bolean = 1; for ( @strings ) { if ( substr( $_, $position, 1 ) ne $letter ) { $bolean = 0; last } } $common_letters[ $position ] = $letter if ( $bolean ); + })/gx; print ( $common_letters[ $_ ] || "-" ) for ( 0 .. length $reference );

P. S.: It certainly will challenge on the regex-side as you had intended originally ;-) Lemme know what you think!
  • Comment on Re: Matching or masking specific characters in an array of strings
  • Download Code

Replies are listed 'Best First'.
Re^2: Matching or masking specific characters in an array of strings
by johngg (Canon) on Dec 20, 2008 at 00:21 UTC

    You are going to get one extra hyphen because

    print ( $common_letters[ $_ ] || "-" ) for ( 0 .. length $reference );

    should be

    print ( $common_letters[ $_ ] || "-" ) for ( 0 .. length( $reference ) + - 1 );

    Cheers,

    JohnGG

      Thanks for pointing this out, johngg!

      You are, of course, perfectly right ... I apologise for overlooking this detail. As regards the overall approach of my code, however, I am glad to report that this correction changes nothing.