in reply to Output

What does your data structure look like, when it's read into your program? If you have categories in an array, it's not too complicated:
while (@lines) { $output .= "<TR>"; for ( 1 .. 3 ) { $output .= "<TD>" . (shift @lines) . "</TD>"; } $output .= "</TR>"; }
You might use the following to make sure you have an appropriate number of things in @lines:
my $fill = scalar @lines % 3; while ($fill-- > 0) { push @lines, " "; }