4

I have a 2D array. I can get a given column of it with the following:

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

Then I can manipulate @column_zero at will.

But how do I place it back into the two dimensional array?

2
  • Do you operate on rows too? if not, perhaps you'd be better off swapping your rows and columns in your array, to match Perl's row-major ordering. Commented Dec 23, 2012 at 18:52
  • I operate on everything. Commented Dec 23, 2012 at 19:11

2 Answers 2

4

Perhaps it's better to get references to the values instead:

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

... so you can manipulate these values directly: you just need to remember that there are references stored in this array, so they should be dereferenced. For example:

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[$_];
}
Sign up to request clarification or add additional context in comments.

Comments

1

TMTOWTDI so

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.