Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Here's my stab at it:
#! /usr/bin/perl use strict; use warnings; use Text::Template; use List::MoreUtils qw( natatime ); my %cmd_data = ( ... ); our $max_sf_cols = 5; my $template = Text::Template->new( TYPE => 'FILEHANDLE', SOURCE => \* +DATA ) or die "Couldn't construct template: $Text::Template::ERROR"; for my $cmd ( sort keys %cmd_data ) { local our $opcode = $cmd_data{$cmd}{opcode}; my @sf_names; push @sf_names, $_->{subfield_name} for @{ $cmd_data{$cmd}{subfields} }; @sf_names = qw( none ) if not @sf_names; my $it = natatime $max_sf_cols, @sf_names; print $template->fill_in() while local our @sf_cols = $it->(); } __DATA__ <p> <table border="1"> <thead> <tr> <th>opcode</th> <th colspan="{ $max_sf_cols }" align="center">subfield(s)</th> </tr> </thead> <tbody> <tr> <td>{ $opcode }</td> { $OUT .= "<td>$_</td>" for @sf_cols } </tr> </tbody> </table> </p>

Probably the most interesting part of it is the List::MoreUtils::natatime (n at a time) function. This almost does the same as jethro's splice, but without changing your data structure. It gives you an iterator sub, and every time you call that iterator sub it gives you the next five items from an array. You can use this in your existing code without bothering much with the other parts.

The templating is just an extra. You can see the template in the DATA section, where every { perl code } section will be replaced by the value of its last statement, or with the value of $OUT if you appended something to it. Notice the scoping of the variables used both in the template and in the main code. They are not lexicals, because you cannot see lexicals of the main code in the templating code fragments.

I'm not dealing much with HTML templating, so I'm sure there are better ways to do this, but I hope this helps.


In reply to Re: How to approach this problem, or should I ignore it? by rubasov
in thread How to approach this problem, or should I ignore it? by jedikaiti

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (5)
    As of 2014-11-26 05:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (162 votes), past polls