in reply to Format a list with commas and "and"

sub serial(@) { join(', ', @_[0..$#_-1]) . (@_>2 ? ',':'' ) . (@_>1 ? (' and ' . $_[-1]) : $_[-1]); }

Replies are listed 'Best First'.
Re: Re: Format a list with commas and "and"
by BrowserUk (Patriarch) on Oct 02, 2002 at 09:43 UTC

    Hey! You can't see the join's :)

    #! perl -sw use strict; sub npAndAList{$"=', '; return unless @_; @_==1 and "@_" or "@_[0 .. $ +#_-1] and $_[-1]" } print npAndAList(qw(a b c d e)[0..$_]),$/ for -1..4; __DATA__ a a and b a, b and c a, b, c and d a, b, c, d and e

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!