in reply to In seach of a more elegant foreach
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: In seach of a more elegant foreach
by kiat (Vicar) on Jun 16, 2004 at 02:11 UTC |