Re: get a column out of a 2d array
- From: tom@xxxxxxxxxxxxxx (Tom Phoenix)
- Date: Wed, 3 Oct 2007 17:22:44 -0700
On 10/3/07, Mahurshi Akilla <mahurshi@xxxxxxxxx> wrote:
is there an easy way (without iterating through the array) to get a
specified column out of a 2d array?
There is no way in general, easy or hard, to get a column out of an
array in Perl without using iteration, even if the iteration is hidden
inside a map, foreach, or something similar. So, your answer is "no".
i know you can get it with rows using ( $my2ndrowref = @array[1] ) but
i don't want to transpose all my data right now for this.
I think you mean something like this:
my @second_row = @{ $array[1] };
print "Second row: @second_row\n";
But now you want a column, instead of a row. Well, if there actually
is a column there (we'll assume your data is rectangular), it's easy
to extract a copy with map:
my @second_col = map $_->[1], @array;
print "Second column: @second_col\n";
That's a copy, instead of the original data. If you want to modify it,
there's probably a better way to do that.
Does that do anything good for you? Good luck with it!
--Tom Phoenix
Stonehenge Perl Training
.
- References:
- get a column out of a 2d array
- From: Mahurshi Akilla
- get a column out of a 2d array
- Prev by Date: Re: Limiting a program to a single running instance
- Next by Date: Re: get a column out of a 2d array
- Previous by thread: get a column out of a 2d array
- Next by thread: Re: get a column out of a 2d array
- Index(es):
Relevant Pages
|