Warm greetings from the backwoods of Pennsylvania.

I've created a very simplistic example that should illustrate my question. Basicaly what I'm trying to do is have my loops interrupted at regular intervals.

If I were to do this in perl only, I'd have a main loop, with a test for "if $count % 2". If that test was true, I'd do the special operations and then continue.

However, working in Template Toolkit, I don't seem to have access to the modulus operator (or, I don't understand the syntax for IF). When the following template runs, I get nothing. When I take the 'IF' block out, I get my list, but of course without the separators which I'm trying to get.

How would one go about making an insertion at a regular interval?

Update: I am embarrased to admit that I fell for the old '=' vs. '==' trick. rafl hit it right away. I'm trying to tell myself I missed it 'cause my eyes weren't looking for Perl syntax, but I'm still *really* ashamed.

Code fixed.

#!/usr/bin/perl use strict; $|++; use Template; my $tt = Template->new; my %data = ( bar_every => 2, colors => [qw( red orange yellow green blue indigo violet silver g +old )], ); $tt->process(\*DATA, \%data); exit; # To make the template below work (exept for inserting the bars), # Just delete or comment the 'IF' and the two following lines __DATA__ [% cnt = 0 %] We're going to print a bar every [% bar_every %] items. [% FOREACH color = colors -%] [% cnt = cnt + 1 -%] [%cnt %] The color is [% color %]. [% IF cnt % bar_every == 0 -%] ---- [% END -%] [% END -%]

In reply to TemplateTookit: Regular Intervals in a FOREACH constuct by pboin

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.