You have a natural refactoring, lets call it cell_wrap,
Since you're just choosing between values in your conditional, the trinary op ?: comes to mind. Finally, I'd keep the foreach loop pretty much as you have it, though I'll pare it down to for and name no temporaries.sub cell_wrap { join '', map {"<tr><td>$_</td></tr>"} @_; }
for (@$matrix_ref) { $html_str .= cell_wrap( $_->[0] eq $key ? $_->[2] : (@{$_}[0..2]) ); $key = $_->[0]; }
It would be a bigger improvement to separate logic from presentation by merging the records and then feeding $matrix_ref to a template engine like Html::Template or TT2.
Update: Arrgh! You changed your presentation code! Take this as another argument for templating.
After Compline,
Zaxo
In reply to Re: In seach of a more elegant foreach
by Zaxo
in thread In seach of a more elegant foreach
by kiat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |