logo       

RE: Using array indexes: msg#00073

Subject: RE: Using array indexes
 
I just started using TT and had a question about iterating over an array. I want to put a fixed number of array items on a HTML row. Something like the following:

# Have an array of hash:
my @array = map {{id=>"abc$_"}} (1..99);
my $aref = \@array;
my $vars = {
  aref => $aref
};

# Want to mimic this in template:
for (0..$#{@$aref}) {
  print "<tr>" if $_ == 0;
  print "<td>$aref->[$_]{id}</td>";
  print "</tr>\n<tr>" unless ($_+1)%4;
  print "</tr>" if $_ == $#{@$aref};
}

# To get this output:
<tr><td>abc1</td><td>abc2</td><td>abc3</td><td>abc4</td></tr>
<tr><td>abc5</td><td>abc6</td><td>abc7</td><td>abc8</td></tr>
<tr><td>abc9</td><td>abc10</td><td>abc11</td><td>abc12</td></tr>
<tr><td>abc13</td><td>abc14</td><td>abc15</td><td>abc16</td></tr>

I know how to do a simple iteration using [% FOREACH aref %] and I know about aref.size which would give the equivalent of scalar(@$aref) but I'm not sure how to accomplish the above. Any help would be great. 
The "Table" plugin suits well for this case. You should use "inverted" table (rows becomes cols and vice versa) to get desired output. So rows=3 here really means number of columns.
 
[% USE table(aref, rows=3) %]
<table>
[% FOREACH cols = table.cols %]
    <tr>
    [% FOREACH item = cols %]
        <td>[% item %]</td>
    [% END %]
    </tr>
[% END %]
</table>
 

--
Sergey Martynoff

 
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
boot-loaders.gr...    php.pear.genera...    debugging.valgr...    kde.redhat.user...    text.xml.xsl.ge...    culture.languag...    hardware.microc...    java.servicemix...    redhat.release....    web.zope.plone....    user-groups.lin...    opendarwin.webk...    video.mjpeg.use...    sysutils.bcfg2....    encryption.gpg....    lx-office.devel...    xfree86.forum/2...    mail.mutt.devel...    acpi.devel/2003...    qnx.openqnx.dev...    network.irc.irs...    freebsd.devel.m...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe