The problem with goblins is that they always come in hordes. This reply inspired me to see if I could take on a horde of goblins, and a slew of other possible scenarios. This is definitely brutal and ugly.
#!/usr/bin/perl
use strict;
use warnings;
my @emails = (
'foo@barcom', 'longontheleft@foo.com',
'foo@longontheright.com', 'equalonbothsides@equalonbothsides',
'almostequal@almostequa', 'a.horde.of.goblins@under.the.floorboard
+s.com',
'goblins@under.the.floorboards.com'
);
my $MAXLENGTH = 16;
my $NUMDOTS = 3;
my %emails;
foreach my $email (@emails) {
$emails{$email}{'length'} = length($email);
if ( $emails{$email}{'length'} <= $MAXLENGTH ) {
print "No need to shorten $email\n";
next;
}
( $emails{$email}{'left'}, $emails{$email}{'right'} ) =
split( '@', $email );
if ( ( ( $NUMDOTS + length( $emails{$email}{'right'} ) ) > ($MAX
+LENGTH) )
&& ( ( $NUMDOTS + length( $emails{$email}{'left'} ) ) > ($MAXL
+ENGTH) ) )
{
print "Shorten both sides\n";
# Shorten the left side
substr(
$emails{$email}{'left'},
$MAXLENGTH - ( $MAXLENGTH / 2 ) - $NUMDOTS - 1,
length( $emails{$email}{'left'} )
)
= '.' x $NUMDOTS;
# Shorten the right side
substr(
$emails{$email}{'right'},
$MAXLENGTH - ( $MAXLENGTH / 2 ) - $NUMDOTS - 1,
length( $emails{$email}{'right'} )
)
= '.' x $NUMDOTS;
}
elsif (
(
length( $emails{$email}{'left'} ) -
length( $emails{$email}{'right'} )
) > $NUMDOTS
)
{
print "Shortening left side\n";
substr(
$emails{$email}{'left'},
$MAXLENGTH - length( $emails{$email}{'right'} ) - $NUMDOTS
+ - 1,
length( $emails{$email}{'left'} )
)
= '.' x $NUMDOTS;
}
else {
print "Shortening right side\n";
substr(
$emails{$email}{'right'},
$MAXLENGTH - length( $emails{$email}{'left'} ) - $NUMDOTS
+- 1,
length( $emails{$email}{'right'} )
)
= '.' x $NUMDOTS;
}
$emails{$email}{'short'} =
join( '@', $emails{$email}{'left'}, $emails{$email}{'right'} );
print "Shortened version ", $emails{$email}{'short'}, " is ",
length( $emails{$email}{'short'} ), " characters long.\n";
}
1;
__END__
No need to shorten foo@barcom
Shortening left side
Shortened version longo...@foo.com is 16 characters long.
Shortening right side
Shortened version foo@longonthe... is 16 characters long.
Shorten both sides
Shortened version equa...@equa... is 15 characters long.
Shortening right side
Shortened version almostequal@a... is 16 characters long.
Shorten both sides
Shortened version a.ho...@unde... is 15 characters long.
Shortening right side
Shortened version goblins@under... is 16 characters long.
-
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.