mascip has asked for the wisdom of the Perl Monks concerning the following question:
Hi all, I want a display_2d() function:
I just wrote this code:my @accounts = [ [ current => 1000 ], [ savings => 2000 ], [ other => 500 ], ]; display_2d(\@accounts); # Should display: current | 1000 savings | 2000 other | 500
and that felt unnecessary. Is there no CPAN module to do this? I'm pretty sure I'm not the only one needing this function.my $width_col = 8; say join ' | ', map { # Give each column the same size length($_) >= $width_col ? $_ : $_ . " "x($width_col-length($_)) # add spaces } @$_ for @rows;
I found Array::Columnize but it only displays one dimensional arrays. And flattening my array does not do the trick: everything ends up displayed in the wrong order. My solution with Array::Columnize ended up being almost as long as my solution without it. And allows for less flexibility (columns of different width, for example)
And Array::PrintCols makes my terminal bug for a minute and then display nothing.
Have you come across dispay_2d() anywhere on CPAN?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing a 2D array into constant-sized columns
by Happy-the-monk (Canon) on Apr 01, 2014 at 11:24 UTC | |
by mascip (Pilgrim) on Apr 01, 2014 at 11:53 UTC | |
|
Re: [Solved] Printing a 2D array into constant-sized columns
by hazylife (Monk) on Apr 01, 2014 at 15:01 UTC | |
|
Re: [Solved] Printing a 2D array into constant-sized columns
by Laurent_R (Canon) on Apr 01, 2014 at 17:52 UTC | |
by mascip (Pilgrim) on Apr 02, 2014 at 11:00 UTC | |
by Laurent_R (Canon) on Apr 02, 2014 at 17:49 UTC | |
by mascip (Pilgrim) on Apr 03, 2014 at 15:14 UTC | |
by Laurent_R (Canon) on Apr 03, 2014 at 17:20 UTC |