Referring to a 'column' from a reference to an array of array references

F

fatted

The DBI module has a function fetchall_arrayref which returns a
reference to an array of references to arrays. Its quite easy to refer
to a row of the matrix:

$row_ref = $array_ref->[0];

(which would correspond to 1 row retrieved from database table), but I
can't think of how to refer to a column of the matrix (corresponds to
all the various values for a particular field / column).
Is it possible to refer to a column using array referencing of some
description? (I know how to iterate over the array and suck out each
column value, I'm just wondering whether there is as easy a way as
when referring to rows). I'd normally iterate using a
foreach/while/for loop or maybe a map function, can anyone think of
alternatives (some sort of array slice or something?).

Cheers,
 
A

Anno Siegel

fatted said:
The DBI module has a function fetchall_arrayref which returns a
reference to an array of references to arrays. Its quite easy to refer
to a row of the matrix:

$row_ref = $array_ref->[0];

(which would correspond to 1 row retrieved from database table), but I
can't think of how to refer to a column of the matrix (corresponds to
all the various values for a particular field / column).
Is it possible to refer to a column using array referencing of some
description? (I know how to iterate over the array and suck out each
column value, I'm just wondering whether there is as easy a way as
when referring to rows). I'd normally iterate using a
foreach/while/for loop or maybe a map function, can anyone think of
alternatives (some sort of array slice or something?).

Wait for Perl 6 for multi-dimensional array slices. In Perl 5 you'll
need a loop to extract a row: "map $_->[ $col], @$array_ref".

Anno
 
J

James Willmore

On 23 Oct 2003 01:38:39 -0700
The DBI module has a function fetchall_arrayref which returns a
reference to an array of references to arrays. Its quite easy to
refer to a row of the matrix:

$row_ref = $array_ref->[0];

(which would correspond to 1 row retrieved from database table), but
I can't think of how to refer to a column of the matrix (corresponds
to all the various values for a particular field / column).
Is it possible to refer to a column using array referencing of some
description? (I know how to iterate over the array and suck out each
column value, I'm just wondering whether there is as easy a way as
when referring to rows). I'd normally iterate using a
foreach/while/for loop or maybe a map function, can anyone think of
alternatives (some sort of array slice or something?).

Well ... you could use 'fetchrow_hashref' in a loop instead of
fetching all the rows at once. Not exactly what you are looking for,
but using 'fetchall_hashref', AFAIK, will only fetch for a single
field by name - unless that's what you want..

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
You may have heard that a dean is to faculty as a hydrant is to a
dog. -- Alfred Kahn
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top