in reply to Format and Join an Array in a Template

It looks like you want to do the same thing to each item in the list except the last item, right? I think:

[% FOREACH n in names.slice(0,-2) %] .[% n %]([% n %]), [%- END %] .[% names.last %]([% names.last %])

Would do what you want. See Template::Manual::VMethods for more. The above is untested, so play around until it actually works.

Update: s/slice(0,-1)/slice(0,-2)/

Replies are listed 'Best First'.
Re^2: Format and Join an Array in a Template
by ikegami (Patriarch) on Jun 19, 2009 at 17:26 UTC

    It fails if the list has zero elements. It's getting complicated fast, which is why I didn't post that solution.

Re^2: Format and Join an Array in a Template
by mscharrer (Hermit) on Jun 19, 2009 at 17:09 UTC
    Thanks, this works (with .slice(0,-2)). I was simply thinking the wrong way. The fact that the format must be used twice is a little drawback, but I think it does not get much shorter.

    It is a pity that the Template module does not provide a 'map' virtual method, otherwise a 'list.map(...).join(...)' would be possible.

      The fact that the format must be used twice is a little drawback,

      You could use a [% BLOCK %] to remove the redundancy.