How can I output a list as comma-separated values in Perl?

Mike picture Mike · May 21, 2010 · Viewed 15.9k times · Source

Let's say I have a list of elements

@list=(1,2,3);
#desired output
1,2,3

and I want to print them as comma separated values. Most importantly, I do not want the last element to have a comma after it.

What is the cleanest way to do this in Perl?

Answer

WhirlWind picture WhirlWind · May 21, 2010
print join(',', @list), "\n";