in reply to [Solved] Printing a 2D array into constant-sized columns
Output:use strict; use warnings; my @accounts = [ [ current => 1000 ], [ savings => 2000 ], [ other => 500 ], ]; foreach my $sub_ref (@accounts) { printf "%-10s | %10d\n", $_->[0], $_->[1] for @$sub_ref; }
Update: Seeing hazylife's EDIT in the code section, it came to my mind that my above code could also be simplified to:current | 1000 savings | 2000 other | 500
Please also note that my code might look slightly more complex (nested loops) because it is working on the OP's original AoA structure, whereas other solutions presented so far were just working on simple arrays.foreach my $sub_ref (@accounts) { printf "%-10s | %10d\n", @$_ for @$sub_ref; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: [Solved] Printing a 2D array into constant-sized columns
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 |