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:
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 ...).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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |