Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Shorten email address

by robartes (Priest)
on Jun 10, 2005 at 07:17 UTC ( #465452=note: print w/ replies, xml ) Need Help??


in reply to Shorten email address

One other, fairly brutal and ugly, way of doing this is by playing substr games:

#!/usr/local/bin/perl -w use strict; my $email='goblins@under.the.floorboards.com'; my $length=15; my $result= eval { substr($email, $length-3) = '...'; return $email }; print $@?$email:$result;

CU
Robartes-


Comment on Re: Shorten email address
Download Code
Re^2: Shorten email address
by redhotpenguin (Deacon) on Jun 10, 2005 at 08:57 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://465452]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-12-26 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls