Well, it's not that difficult to do in a template (but alas, I've done this sort of things many times):
use 5.020; use warnings; use Template; my $data = [[ qw(1 2 A 4) ], [ qw(3 4 A 5) ], [ qw(5 5 B 2) ], [ qw(6 2 B 3) ], [ qw(1 2 C 2) ], [ qw(2 3 C 2) ], [ qw(1 3 C 1) ], ]; my $template = <<'EOT'; [% SET category = ''; FOREACH line IN data; IF line.2 != category; GET line.2; "\n"; category = line.2; END; GET line.join(" "); "\n"; END; %] EOT my $tt = Template->new; $tt->process(\$template,{data => $data});
That said, if you're munging your data structure in Perl, then I'd suggest a different structure with a hash as the top level, and lists of lists inside:
{ A => [[qw(1 2 A 4)],[qw(3 4 A 5)]], B => [[qw(5 5 B 2)],[qw(6 2 B 3)]], C => [[qw(1 2 C 2)],[qw(2 3 C 2)],[qw(1 3 C 1)]], }
...which can then be processed with a different template. Note the extra sort which is required because hash entries come in random order.
use 5.020; use warnings; use Template; my $data = [[ qw(1 2 A 4) ], [ qw(3 4 A 5) ], [ qw(5 5 B 2) ], [ qw(6 2 B 3) ], [ qw(1 2 C 2) ], [ qw(2 3 C 2) ], [ qw(1 3 C 1) ], ]; my %munged; for my $record (@$data) { no warnings "uninitialized"; push @{$munged{$record->[2]}}, $record; } my $template = <<'EOT'; [% FOREACH category IN data.keys.sort; GET category; "\n"; FOREACH record IN data.$category; GET record.join(" ");"\n"; END; END; %] EOT my $tt = Template->new; $tt->process(\$template,{data => \%munged});

In reply to Re: Control break with Template::Toolkit by haj
in thread Control break with Template::Toolkit by betacentauri

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.