OfficeLinebacker has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, just out of curiosity, is there a better way to do this?
@emaillist=split(/,/,$ARGV[1]); my $distlist; foreach $k (@emaillist){ $distlist="$distlist $k\@myorg.mydomain"; }

Edit: Looks like some people do not like this node. Can you please explain why this question or presentation is not appropriate?

Also thanks for the replies. I have never used map(). Will look into that.

Edit #2: The approach above also has the shortcoming of inserting a space in front of the first address, which in my particular case caused a VERY unpleasant side effect. Just FYI for anyone else who may stumble upon this later.

  • Comment on Turning a comma-delimited list of internal email addresses into a space-delimited list of internet email addys?
  • Download Code

Replies are listed 'Best First'.
Re: Turning a comma-delimited list of internal email addresses into a space-delimited list of internet email addys?
by TedPride (Priest) on Apr 28, 2006 at 20:17 UTC
    Maybe:
    my $distlist = join ' ', map { "$_\@myorg.mydomain" } @emaillist;
    Or if you want to transform the original array at the same time:
    my $distlist = join ' ', map { $_ = "$_\@myorg.mydomain" } @emaillist;
Re: Turning a comma-delimited list of internal email addresses into a space-delimited list of internet email addys?
by dragonchild (Archbishop) on Apr 28, 2006 at 20:18 UTC
    my $distlist = join ' ', map { "$_\@myorg.mydomain" } split /,/, $ARGV +[1];
    Of course, some people would find that obfuscated. Personally, I find your version harder to read. So, pick whichever is appropriate for the people expected to maintain this.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Turning a comma-delimited list of internal email addresses into a space-delimited list of internet email addys?
by Herkum (Parson) on Apr 28, 2006 at 20:21 UTC
    How about this,
    my @addresses = split q{,}, $ARGV[1]; foreach my $address (@addresses) { $address .= q{@myorg.mydomain}; } my $list = join q{ }, @addresses;
    It is not shorter, but I think it is clearer because I am only doing one thing at a time. In your foreach loop you are basically do a fancy concat with spaces and adding the '@myorg.domain' all the same line. It is kinda hard to see that just by looking at it.
Re: Turning a comma-delimited list of internal email addresses into a space-delimited list of internet email addys?
by Fletch (Bishop) on Apr 28, 2006 at 20:18 UTC

    You could explicitly use the concatenation operator . and do $distlist .= " $k\@myorg.mydomain".

    You could build an array and join the items after the fact.

    my @addrs; for my $x ( @emaillist ) { push @addrs, "$k\@myorg.mydomain"; } my $distlist = join( " ", @addrs );

    You could use map and join and do it all in one swell foop.

    my $distlist = join( " ", map { "$_\@myorg.mydomain" } split( /,/, $AR +GV[1] ) );

    Depends on one's definition of better.

      Great answer, and LOL@ one swell foop!
Re: Turning a comma-delimited list of internal email addresses into a space-delimited list of internet email addys?
by eff_i_g (Curate) on Apr 28, 2006 at 20:19 UTC
    #!/usr/bin/perl -w use strict; print my $dist_list = join ' ' => map { "$_\@myorg.mydomain" } split / +,/ => $ARGV[1]; print "\n";
    Or use warnings; if you have a newer perl.