如何一次循环列表四个元素?

cap*_*ser 2 perl loops

这是来自其他成功脚本的切片.我需要做的是打印一个<tr>然后打印<td>$stat_array</td>四次,退出,打印</tr>再打印一份<tr>,打印在未来四年$stat_arrays@stat_array,等等,然后一个</table>.

它的作用是打印所有八个$start_arrays然后a </tr>.

my @table_header = ("Process", "Region_Permission","Region Violation","Message Type");
my @stat_array =("ibfarm102  - localtick" ," Greenwich" ," hibmis100 -  procHKHD2 -     Hongkong" , "PidMonRsp" ," ibfarm102  - localtick", "Greenwich" ,"hibmis100 -      procHKHD2 - Hongkong", "PidMonReq");

print MAIL "<tr>\n";

for ($i = 0 ; $i <$#table_header ; $i = $i + $#table_header) {
    foreach my $stat_array(@stat_array) {
        print MAIL "<td>$stat_array</td>\n";
    }

    print MAIL "</tr>\n";
}
print MAIL "</table>\n";
print MAIL "<br><br>\n";
print MAIL "</table></center></body></html>";
close MAIL;
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

<tr>
<td>ibfarm102  - localtick </td>
<td> Greenwich</td>
<td> hibmis100 -  procHKHD2 - Hongkong </td>
<td>PidMonRsp</td>
<td>ibfarm102  - localtick </td>
<td> Greenwich</td>
<td> hibmis100 -  procHKHD2 - Hongkong </td>
<td>PidMonReq</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

我需要表格看起来像:

<tr>
<td>ibfarm102  - localtick </td>
<td> Greenwich</td>
<td> hibmis100 -  procHKHD2 - Hongkong </td>
<td>PidMonRsp</td>
</tr>
<tr>
<td>ibfarm102  - localtick </td>
<td> Greenwich</td>
<td> hibmis100 -  procHKHD2 - Hongkong </td>
<td>PidMonReq</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

Zai*_*aid 8

听起来像List::MoreUtils' natatime(n-at-a-time)函数的好候选人:

use strict;
use warnings;
use List::MoreUtils 'natatime';

my $four_at_a_time = natatime 4, @stat_array;
my $string_to_print = "<html><body><center><table>\n";

while ( my @four = $four_at_a_time->() ) {

    $string_to_print .= join "\n", "<tr>",
                                   map { "<td>" . $_ . "</td>" } @four,
                                   "</tr>\n";
}
Run Code Online (Sandbox Code Playgroud)