It's quite common when outputting tabular data (be it CSV, or an HTML table) to ensure that column headers are printed once by using something along the lines of:
sub arrayref_to_csvline { ... } my $headers; while (my $row = $iter->()) { unless ($headers) { print arrayref_to_csvline($row->fields); $headers = 1; } print arrayref_to_csvline($row->values); }
I recently discovered a pattern that takes advantage of the return value of print (it returns 1 on success)...
sub arrayref_to_csvline { ... } my $headers; while (my $row = $iter->()) { $headers ||= print arrayref_to_csvline($row->fields); print arrayref_to_csvline($row->values); }
... just thought I'd share.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing the header row
by martin (Friar) on Sep 13, 2012 at 15:48 UTC |