Okay, so your question is basically "How do I edit a string of comma delimited email addresses"?
One way is to separate the data. Split the string on commas (assumes no email address have commas in them), then split each of those elements into outside <> and inside <>
Edit the data, then rebuild your string.
This seems to work:
#!/usr/bin/perl -w
use strict;
my $test='A <a@bob.com>, B <B@b.org>, Calcutta <O@thehorror.net>';
my @names = split /\s?,\s?/, $test; #split apart
$_ = [split /\s(?=<)/, $_] for (@names); #make array refs of each
# Above happily learned from merlyn
foreach my $name (@names) {
foreach (@{$name}){
print "--$_--";
}
print "\n";
}
It outputs:
--A----<a@bob.com>--
--B----<B@b.org>--
--Calcutta----<O@thehorror.net>--
And of course, you can edit that as you wish. Of course, if you KNOW one element, it'd be better to use a hash
#!/usr/bin/perl -w
use strict;
my $test='A <a@bob.com>, B <B@b.org>, Calcutta <O@thehorror.net>';
my %names = split /,\s|\s(?=<)/, $test; #split apart
foreach my $name (keys (%names)) {
print "$name==$names{$name}\n";
}
This also works, and allows you to access (and modify) $names{Joe Schmoe}
Rebuilding your original list is even easier after you modify it.
my @names;
while (my ($key, $value) = each %names){
push @names, "$key $value";
}
print join ', ',@names;
Note that all of the above code assumes that:
- No email addresses or names have commas
- No names or email address have < beyond the wrapper around email addresses
- Your list always has the entries separated with a comma then a space
- Your names are always separated from your email addresses by a space.
|