in reply to Multi dimentions to a single dimentional array
However you can use shorter and cleaner constructs, if you don't need them one-by-one, but all of them at once, as an array for the "column". Try this:use strict; use warnings; my @AoA = ( [ 'Jan', 10, 20 ], [ 'Feb', 15, 25 ], [ 'Mar', 30, 33 ] ); my @column; for my $month_data (@AoA) { push @column, $month_data->[0]; } print join( ' ', @column ), "\n";
The map expression above (map {$_->[0]} @AoA) creates and returns a new array by implicitly iterating through your array refs ("lines", $AoA[0], $AoA[1] ...), taking the first element ("column", $AoA[0][0], $AoA[1][0] ...) from each and constructing the new array from them. (But of course, if you think this is too much for once, just stick with the first and easier version.)use strict; use warnings; my @AoA = ( [ 'Jan', 10, 20 ], [ 'Feb', 15, 25 ], [ 'Mar', 30, 33 ] ); print join( ' ', map {$_->[0]} @AoA ), "\n"; # prints 'Jan Feb Mar' print join( ' ', map {$_->[1]} @AoA ), "\n"; # prints '10 15 30'
|
|---|