in reply to DBI select ids to @ids

how about
@ids = @{$dbh->selectcol_arrayref('select id from table where foo > 42 +', { Columns => [1]})};
update

tomazos is right, you can get rid of the Columns if you are only selecting one column
@ids = @{$dbh->selectcol_arrayref('select id from table where foo > 42 +')};

Replies are listed 'Best First'.
Re^2: DBI select ids to @ids
by tomazos (Deacon) on Aug 10, 2005 at 05:47 UTC
    If this works, and the manual says it defaults to one, you can drop the \%attr column and just write @ids = @{$dbh->selectcol_arrayref('select id from table where foo > 42')};

    Why isn't there a $dbh->selectcol_array? There is a $dbh->selectrow_array. Thats weird.

    It should be just:

    @ids = $dbh->selectcol_array('select id from items where foo > 42'); +# SHOULD WORK BUT WRONG

    Or am I missing something?

    -Andrew.


    Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com
      Why no selectcol_array?
      I'm not sure. You could always email Tim Bunce and ask him.
      However, dereferencing an array reference is easy. I really don't see a need for it.
      Personally, I wouldn't dereference it into an array. You're not showing how your are using the @ids array but if you need to pass it around in your code, say as a parameter to another sub, you will probably want it as a reference anyway.
        I agree that wrapping the whole call in @{} isn't hard, however amongst a large chunk of code its easy to mentally parse the brackets incorrectly, or confuse them with end of block delimiters.

        I don't like passing things by reference unless either (A) the sub is going to modify it, or (B) it is so big that it is an efficiency concern. This is because the subroutine might modify it accidentally. Passing by value helps to isolate a bug to a smaller scope (or if the accidental modification doesn't effect the functionality of the sub - avoids it completely).

        Plus its hard to keep track of what is a reference and what isn't, so I try to keep everything lexical and dereferenced to take advantage of the cleaner syntax.

        -Andrew


        Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com