in reply to help me with template module to print like this

So if I'm understanding you correctly, you want to use "or" as a separator before the last entry in the list if the list is longer than 2, and "," otherwise.

The loop iterator supports several methods like .last(), .size(), .count() etc., so you could for example test .last() (which returns true if the current iteration is the last) to determine whether to insert "or" or ","...

See also Loop Processing.

Replies are listed 'Best First'.
Re^2: help me with template module to print like this
by veerubiji (Sexton) on Nov 09, 2011 at 21:37 UTC

    exactly you understand my problem.I want print as you said. I tried using .size also but still problems arising. can you see what tried using .size, I posted in main question.Can you tel what mistake i did and how to print data as i want.

      [% LAST %] is not the same as loop.last().

      The data you've shown is a little too messy for me (to try to fix it up to actually match its usage in the given template...), but the general idea would be something like this:

      my $data = { employee => [ { name => 'Foo' }, { name => 'Bar' }, { name => 'Baz' }, ] }; my $template = Template->new(); $template->process(\*DATA, $data) || die "Template process failed: ", $template->error(), "\n"; __DATA__ [% FOREACH person IN employee %] [%- IF loop.index() %][% loop.size()>2 && loop.last() ? ' or':',' +%] [% END %] [%- person.name %] [%- END %].

      Output:

      Foo, Bar or Baz.

        Hi Eliya, Thank you for providing such example, That is working, my problem is solved. Thank you very much.

        regards, veerubiji.