View Problem

Join the elements of a list, separated by commas

Given the list [Apple, Banana, Carrot] produce "Apple, Banana, Carrot"
DiskEdit
perl
print join ', ', qw(Apple Banana Carrot);
DiskEdit
perl
# Longer and less efficient than join(), but illustrates
# Perl's foreach operator, which can be useful for
# less trivial problems with lists

@list = ('Apple', 'Banana', 'Carrot');
foreach $fruit (@list) {
print "$fruit,";
}
print "\n";
DiskEdit
perl
my @a = qw/Apple Banana Carrot/;
{
local $, = ", ";
print @a
}
print "\n";
DiskEdit
perl
my @a = qw/Apple Banana Carrot/;
{
local $" = ", ";
print "@a\n";
}
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ result = String::Join(L", ", fruit->ToArray());
ExpandDiskEdit
cpp boost
string fruits[] = {"Apple", "Banana", "Carrot"};
string result = boost::algorithm::join(fruits, ", ");
DiskEdit
csharp .NET 3.5
using System.Collections.Generic;
public class JoinEach {
public static void Main() {
var list = new List<string>() {"Apple", "Banana", "Carrot"};
System.Console.WriteLine( string.Join(", ", list.ToArray()) );
}
}

Submit a new solution for perl, cpp, or csharp
There are 22 other solutions in additional languages (clojure, erlang, fantom, fsharp ...)