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

Discussion in 'Perl Misc' started by fatted, Oct 23, 2003.

  1. fatted

    fatted Guest

    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,
     
    fatted, Oct 23, 2003
    #1
    1. Advertising

  2. fatted

    Anno Siegel Guest

    fatted <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Oct 23, 2003
    #2
    1. Advertising

  3. Re: Referring to a 'column' from a reference to an array of arrayreferences

    On 23 Oct 2003 01:38:39 -0700
    (fatted) wrote:

    > 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
     
    James Willmore, Oct 23, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    457
    Karl Heinz Buchegger
    Nov 17, 2003
  2. JKop
    Replies:
    12
    Views:
    722
    Howard
    Jul 21, 2004
  3. Replies:
    3
    Views:
    470
    Victor Bazarov
    Nov 10, 2004
  4. DanielEKFA
    Replies:
    8
    Views:
    626
    DanielEKFA
    May 16, 2005
  5. Saeed Bhuta

    Referring to Hash and Array Elements

    Saeed Bhuta, Feb 24, 2010, in forum: Ruby
    Replies:
    3
    Views:
    112
    Rick DeNatale
    Feb 24, 2010
Loading...

Share This Page