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!
-
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.
|