in reply to DBI select ids to @ids

I think this will do the trick:

use DBI; my $dbh = DBI->connect(...); my $stmt = 'select id from items where foo > 42'; my @ids = map { $_->[0] } @{ $dbh->selectall_arrayref($stmt) };

map is used to convert an array of rows of fields into an array of (what's in the first field of each row).

Replies are listed 'Best First'.
Re^2: DBI select ids to @list
by tomazos (Deacon) on Aug 10, 2005 at 05:43 UTC
    Hmmm. Learning from your example it looks like I might be able to use selectcol_arrayref also:

    @ids = @{ $dbh->selectcol_arrayref($stmt) }

    Or am I wrong here? It looks like selectcol_arrayref has some magic to select multiple columns but the manual says it defaults to the first. But is it a reference to a an array of references to an array, or just a reference to an array?

    -Andrew.


    Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com
      ah yes, selectcol_arrayref would do the trick. That's exactly why it was written, I bet. I had forgotten about that function
      use DBI; my $dbh = DBI->connect(...); my $stmt = 'select id from items where foo > 42'; my @ids = @{ $dbh->selectcol_arrayref($stmt) };