kiat has asked for the wisdom of the Perl Monks concerning the following question:
I'm wondering if there's a more elegant way of achieving what I'm doing with the code below (somewhat related to my node 366914):
Lots of thanks in advance :)my $matrix_ref = [[1, a1, z1],[1, a1, z2], [1, a1, z3], [2, a2, z1], [ +2, a2, z2], [3, a3, z1]]; my ($html_str, $key, $count); # id, a_item, z_item foreach my $row (@$matrix_ref) { if ($row->[0] eq $key) { $html_str .= qq~<tr><td>$row->[2]</td></tr>\n~; } else { $html_str .= '</table>' if $count > 0; $html_str .= qq~<table><tr><td>$row->[0]</td></tr><tr><td>$row->[1 +]</td></tr><tr><td>$row->[2]</td></tr>\n~; } $key = $row->[0]; $count++; } # output 1 a1 z1 z2 z3 2 a2 z1 z2 3 a3 z1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: In seach of a more elegant foreach
by Zaxo (Archbishop) on Jun 16, 2004 at 01:54 UTC | |
by kiat (Vicar) on Jun 16, 2004 at 02:11 UTC | |
|
Re: In seach of a more elegant foreach
by dragonchild (Archbishop) on Jun 16, 2004 at 02:31 UTC | |
by kiat (Vicar) on Jun 16, 2004 at 02:40 UTC | |
|
Re: In seach of a more elegant foreach
by cyocum (Curate) on Jun 16, 2004 at 11:51 UTC | |
|
Re: In seach of a more elegant foreach
by diotalevi (Canon) on Jun 16, 2004 at 14:52 UTC |