The following works fine.

EDIT: While working fine, my previous algorithm didn't preserve as much of the left part of the email as I wanted. The below is simpler and smarter:

use strict; use warnings; my ($ex, $ex1, $ex2, @test); my $length = 14; for $ex1 (1..15) { for $ex2 (5..15) { push @test, 'a' x $ex1 . '@' . 'b' x $ex2; } } for (@test, <DATA>) { chomp; if (length($_) > $length) { $ex = length($_) - $length + 3; @_ = split /@/; if (length($_[1]) > $ex) { substr($_[1], length($_[1]) - $ex, $ex) = '...'; } else { $ex += 3; $ex2 = length($_[1]) - 1; $ex1 = $ex - $ex2; if ($ex1 < 4) { $ex1 = 4; $ex2 = $ex - $ex1; } substr($_[0], length($_[0]) - $ex1, $ex1) = '...'; substr($_[1], length($_[1]) - $ex2, $ex2) = '...'; } $_ = join '@', @_; } print "$_\n"; } __DATA__ ted@dingos.com mary@supercalifradulistic.org i.am.a.purple.dinosaur@barney.must.die.com
EDIT: My previous algorithm determined how many characters needed to be removed. Then it divided those between the two sides of the emai based on how long each side was. If the left part was smaller than 4 characters, it gave everything to the right. If the right part was smaller than 4, it gave everything to the left. If both sides were in use, it added 3 characters and recalculated (since you needed two sets of ...).

The problem with this approach is it took away too much from the left side, which you want preserved if at all possible. My second algorthm (seen above), gives everything to the right side unless the right side is too short, in which case it adds 3 and gives the extra to the left, and if the left is too short for that, it gives 4 to the left and the rest to the right. I included all significant test cases so you can see what it's doing.


In reply to Re: Shorten email address by TedPride
in thread Shorten email address by redhotpenguin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



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