Perl places an array into a 2D array in a specific column

I have a 2D array. I can get it in the following column:

my @column_zero=map {$_->[0]} @{$twod_array}; 

Then I can manipulate @column_zero as desired.

But how do I put it back in a two-dimensional array?

+4
source share
2 answers

It might be better to get references to the values:

 my @ref_to_column_zero = map { \($_->[0]) } @{$twod_array}; 

... so you can directly manipulate these values: you just need to remember that there are links in this array, so they must be dereferenced. For instance:

 for (@ref_to_column_zero) { ${ $_ } *=2; } 

If you prefer to use the old approach, you can do this:

 for (0..$#column_zero) { $twod_array->[$_][0] = $column_zero[$_]; } 
+4
source

TMTOWTDI so

 do{my $i; $twod_array[$i++][0] = $_ for @column_zero;}; 
+1
source

All Articles