Re: Splitting a string, Just want to Keep Half
by hardburn (Abbot) on Jul 06, 2003 at 00:56 UTC
|
use Mail::Address;
my $addr = Mail::Address->parse('user@example.com');
my $domain = $addr->host();
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] [d/l] |
Re: Splitting a string, Just want to Keep Half
by Zaxo (Archbishop) on Jul 06, 2003 at 00:15 UTC
|
You're sooo close:
(undef, my $domain) = split /\@/, $email;
or else:
my $domain = (split /\@/, $email)[1];
Update: Pay attention to Limbic~Region and hardburn, email addresses are more than your code or mine admits. I only corrected your syntax.
After Compline, Zaxo
| [reply] [d/l] [select] |
Re: Splitting a string, Just want to Keep Half
by ctilmes (Vicar) on Jul 06, 2003 at 00:11 UTC
|
my $domain = (split(/\@/,$email))[1];
print "Domain is $domain\n";
| [reply] [d/l] |
Re: Splitting a string, Just want to Keep Half
by broquaint (Abbot) on Jul 06, 2003 at 00:11 UTC
|
my $domain = (split '@', 'dru@perlmonks.org')[-1];
print "Domain is $domain\n";
__output__
Domain is perlmonks.org
HTH
_________ broquaint | [reply] [d/l] |
Re: Splitting a string, Just want to Keep Half
by Limbic~Region (Chancellor) on Jul 06, 2003 at 00:28 UTC
|
Dru,
It is commonly thought that a valid email address is far more strict than it actually is. I think email addresseses may even contain more than one @ symbol. This would imply using the limit argument in the split. I do believe there are some fairly decent modules on CPAN for email address stuff if this is going to be used for anything other than just casual personal use.
Ok - chances are you didn't need to hear any of that, so an alternative to split would be to use index and substr.
#!/usr/bin/perl -w
use strict;
my $email = 'joe.snuffy@nowhere.com';
my $domain = substr($email,index($email,'@') + 1);
print "Domain is $domain\n";
Cheers - L~R | [reply] [d/l] |
Re: Splitting a string, Just want to Keep Half
by Aristotle (Chancellor) on Jul 06, 2003 at 12:49 UTC
|
Also note that since you're not actually looking to split the string, this might better be expressed as a pattern match: "I want the part after the @ sign" rather than "I want the second of the string parts delimited by an @ sign".
my ($domain) = ($email =~ /@(.*)/);
print "Domain is $domain\n";
Makeshifts last the longest. | [reply] [d/l] |
|
I would also recommend to use a regex instead of a split as it allows you to check for valid domain name characters :
my ($domain) = ($email =~ /@([a-zA-Z0-9.-]+)$/);
if( defined $domain ) {
print "Domain is $domain\n";
} else {
print "Invalid or missing domain name ...\n";
}
| [reply] [d/l] |
Re: Splitting a string, Just want to Keep Half
by David Caughell (Monk) on Jul 06, 2003 at 22:22 UTC
|
Hey!
I'm a bit of a newb to this language (though I'm ambitious... currently I'm working on writing a script that makes PHP conform to "use strict;", in a limited sense). :)
I think that the people who've written comments on this so far have done some beautiful code, which is true to the intent of Perl, but isn't necessarily what's required for a beginner working on this project.
If I were you, (and I wanted to follow this line of thinking in terms of splitting the email addy into an array) I would do the following:
use strict;
use warnings; # as always!
my $emailaddy = "grasp_the_sun\@notme.com";
my @emailarr = split /\@/, $emailaddy;
my $domain = pop @emailarr;
Just to give you an idea of how new to the language I am, I tested my original code, and I'd forgotten that I needed to preceed the "@" in the email address with a "\". But the code does work as presented.
Hope this helps!
--Dave. | [reply] [d/l] |
Re: Splitting a string, Just want to Keep Half
by TomDLux (Vicar) on Jul 07, 2003 at 00:16 UTC
|
defined is a verb, to detect whether a scalar has a value.
undef is a noun, to assign a scalar not have a value.
In the code below, some values are true, some false, but all are defined. Then, assigning undef to them means they are no longer defined. Notice that the hash still contains the key a, with no value associated with it; you would need to delete the key to remove the key from the hash.
| [reply] [d/l] |
Re: Splitting a string, Just want to Keep Half
by rajiv7374 (Initiate) on Jul 07, 2003 at 06:04 UTC
|
#!/opt/perl/bin/perl
my $domain;
$email=q{rajiv.gangadharan@hp.com};
($domain) = (split /\@/,$email)1;
print "Domain is $domain\n";
Just take the first (exclude the 0th element which is the name) element and print it. | [reply] |