in reply to Shorten email address

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.

Replies are listed 'Best First'.
Re^2: Shorten email address
by QM (Parson) on Jun 10, 2005 at 18:18 UTC
    The following works fine.
    Sorry, I find Code from God problematic at times. I see a slew of uncommented code, and very short variable names. It also looks difficult to maintain.

    What does it do (or, what do you think it does), and why that instead of something else?

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of