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!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.