I want to create a routine that adds commas to elements and adds "and" to the last element, for example, so that "12345" becomes "1, 2, 3, 4, and 5". I know how to add commas, but the problem is that I get "1, 2, 3, 4, and 5," and I don't know how to get rid of the last comma.
sub commas { my @with_commas; foreach (@_) { push (@with_commas, ($_, ", "));
As you can probably tell, I'm trying to delete the last element in a new array (@with_commas), since it has an added comma and adds the last element to the old array (@_ passed to the subprogram from the main program, without adding a comma).
When I run this, the result is, for example, "1, 2, 3, 4 and 5", with a comma at the end. Where does this comma come from? Only @with_commas was supposed to get commas.
Any help is appreciated.
source share