Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Extracting columns from a two dimensional array

by sasikumar (Monk)
on Dec 21, 2004 at 10:25 UTC ( [id://416424] : note . print w/replies, xml ) Need Help??


in reply to Extracting columns from a two dimensional array

Hi

I was able to get the whole column value from the single row but still for a single column it seems to do wonders. Any idea reagarding it.

my @array=( [0,1,2,3,4], [25,26,27,28,29], [10,11,12,13,14] ); print @{@array[0]}[0..4];

This prints 01234 as required.

But
my @array=( [0,1,2,3,4], [25,26,27,28,29], [10,11,12,13,14] ); print @{@array[0..2]}[3]

This prints 13... The logic is ok but the way arrays are constructed in perl marks this limitation.

Update: I hope this is a limitation in perl. The basic datastructure that is used to store the arrays store the start locatation of the array. In case of multidimentional arrays its the array of arrays so it is stored as a reference to the reference where it points out to the row. Hence we can retrive the row in above mentioned method but not the vice versa.
So it not possible to get the 0..23 from the array without the use of for loop or any other extra logic
Thanks
Sasi Kumar

Replies are listed 'Best First'.
Re^2: Extracting columns from a two dimensional array
by Aristotle (Chancellor) on Dec 21, 2004 at 11:16 UTC

    It's not a limitation of Perl at all. At least for some values of “limitation”.

    The cause of this is that there is no two dimensional array in Perl. You are constructing a representation of such by using an array of arrays, but a one-dimensional array of one-dimensional arrays is not the same as a two dimensional array. There are fewer constraints on such a data structure (the subarrays need not be uniform in length) but in turn, it also loses some expressiveness since you only address one array at a time. You can still represent all operations possible on a two-dimesional array using an array of arrays, but it does require looping.

    To do what the OP wants to do, it would also have to be possible to say @{ \@a, \@b, \@c }[ 3 ], which it is not.

    Makeshifts last the longest.

Re^2: Extracting columns from a two dimensional array
by Chady (Priest) on Dec 21, 2004 at 10:39 UTC

    Here's what's happening; a list in scalar context will return its last element.

    @array[0..2] will return a list, and in @{}'s scalar context, the last element will be returned, which is the last array ref, that's why you get the last array's item 3 only.

    Here's a workaround:

    my @array=( [0,1,2,3,4], [25,26,27,28,29], [10,11,12,13,14] ); print @{@array[$_]}[3] for 0 .. 2;

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
    Chady | http://chady.net/
    Are you a Linux user in Lebanon? join the Lebanese Linux User Group.
Re^2: Extracting columns from a two dimensional array
by Cody Pendant (Prior) on Dec 21, 2004 at 10:48 UTC
    This works nicely in my humble opinion:
    my @array=( [ 0, 1, 2, 3, 4], [25,26,27,28,29], [10,11,12,13,14] ); print @array->[$_]->[3],$/ for (0..3); 3 28 13
    Though it's not exactly what you're asking for.


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
    =~y~b-v~a-z~s; print