extract a column from 2 dimensional array

R

ravi

How to pick a column into a seperate 1-D array, from a 2-D array. Is
there an efficient way to do this or we have to only iterate each
elements
 
G

Gunnar Hjalmarsson

ravi said:
How to pick a column into a seperate 1-D array, from a 2-D array.
Is there an efficient way to do this or we have to only iterate
each elements

Illustrate what you mean by posting a simple example.
 
A

Anno Siegel

ravi said:
How to pick a column into a seperate 1-D array, from a 2-D array. Is
there an efficient way to do this or we have to only iterate each
elements

Unless your array is stored by columns (the specification "2-D array"
is ambiguous), you will have to loop:

map $array->[ $_]->[ $column], 0 .. $#array;

Perl 6 will have multidimensional slices, one hears. Perl 5 doesn't.

Anno
 
A

Andres Monroy-Hernandez

If I understand correctly you have:
my @two_d_array = ( [an, array], [yet, another, one]);

and you want the column $i:
my @one_d_array = map { $_->[$i] } @two_d_array;

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top