in reply to Re: serial comma function
in thread serial comma function

I agree cleaner with pop, but to many ternary operators. :)
sub serial { my $last = pop; @_ ? join(', ', @_) . " and $last" : $last; }
update:forgot about that whole, if there are more than two in the list with the commas bit, so neglect the above as it does not do what the original code posted does (as Aristotle's does.)

-enlil

Replies are listed 'Best First'.
Re^3: serial comma function (not that easy..)
by Aristotle (Chancellor) on Apr 16, 2003 at 00:04 UTC
    I was actually about to reply with something similar myself before I noticed there's three cases to distinguish. FWIW, here's my previous take - which adds a comma even when there's only two elements in the list:
    sub serial { my $last = ''; ($last, $_[-1]) = ($_[-1], 'and ') if @_ > 1; join(', ', @_) . $last; }

    Makeshifts last the longest.