My assumption is that NateTut would like the custom  join to behave analogously to the built-in join: two join-expressions must be present, and either may evaluate to the empty string; the list of strings to join may be empty.

AR's and kennethk's approaches both produce oddball output for corner cases: undefined or oddly joined output, operations on undefined values.

As far as I can tell, the following functions, in increasing order of opacity, all operate like the join built-in. I haven't done any Benchmark-ing on them.

sub join_multi { die "not enough parameters for join_multi()" if @_ < 2; my $delim1 = shift; my $delim2 = shift; return @_ > 1 ? join $delim1, $_[0], join $delim2, @_[1 .. $#_] : @_ ? $_[0] : '' ; } sub join_multi { die "not enough parameters for join_multi()" if @_ < 2; return @_ > 3 ? join $_[0], $_[2], join $_[1], @_[3 .. $#_] : @_ == 3 ? $_[2] : '' ; } sub join_multi { die "not enough parameters for join_multi()" if @_ < 2; return join '', map { ('', $_[0], ($_[1]) x ($#_ - 2))[$_ - 2], $_[$_] } 2 .. $#_ ; }

In reply to Re: join using different delimiter for first element of list by AnomalousMonk
in thread join using different delimiter for first element of list by NateTut

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.