Re: [SPOILER] Perl 'Easy' Quiz #2005-2
Bill Smith <[email protected]> Sat, 05 Feb 2005 11:05:03 -0500
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <000801c50b9c$756cde80$ab00a8c0@hfwk801> |
I am submitting four solutions.
I am not convinced that "maskit"
will work for all input strings,
but I have not found an example
where it does not work correctly.
sub maskit{
my $mask = '#@%';
($_ = $_[0]) =~ s/\.([^.]*)$/$mask$1/;
tr /\.//d;
{ no warnings; s/$mask($1)$/.$1/; }
return $_;
}
Interesting use of functions.
sub reverseit {
$_ = reverse $_[0];
my $i = 1 + index $_, '.';
(my $prefix = substr $_, $i) =~ tr/.//d;
substr( $_, $i, -$i + length $_ , $prefix);
$_ = reverse $_;
}
Very short.
sub oneatatime{
$_ = $_[0];
while (s/\.(.*\..*)/$1/){}
return $_;
}
Recursive version of "very short"
sub recurse{
$_ = $_[0];
if (s/\.(.*\..*)/$1/){
recurse($_)
} else {
return $_;
}
}