variable number of arguments to bind_columns

A

alexxx.magni

Hi,
I have to call the method bind_columns() of DBI, with a number of
arguments unknown at runtime: the args are references, each to a
column value

e.g. with 3 args:
$sth->bind_columns(undef,\$a[0],\$a[1],\$a[2]);

or 4:
$sth->bind_columns(undef,\$a[0],\$a[1],\$a[2],\$a[3]);


there is a way to construct this call in a single way, given the
$ncolumns value?

Thank you!

Alessandro
 
A

alexxx.magni

ok, dumb question (I must be tired)
....
push @bindargs,\$a[1];
....
push @bindargs,\$a[2];
....
$sth->bind_columns(undef,@bindargs);

alessandro



(e-mail address removed) ha scritto:
 
B

Ben Morrow

Quoth Glenn Jackman said:
At said:
ok, dumb question (I must be tired)
...
push @bindargs,\$a[1];

Note that using variables called 'a' and 'b' is a bad idea, as $a and $b
are slightly magic and used by sort. The arrays @a and @b are not
affected by this, but using them is a bad habit to get into.
...
push @bindargs,\$a[2];
...
$sth->bind_columns(undef,@bindargs);

A more compact replacement for 'n' push commands is:
my @bindargs = map { \$_ } @a;

To expand slightly: a more compact replacement for

push @bindargs, $x;
push @bindargs, $y;

is

push @bindargs, $x, $y;

as push takes a list. A more compact replacement for

\$a[1], \$a[2]

is

map { \$_ } @a

or

map \$_, @a

which is marginally more efficient and (IMHO) clearer; or, since we are
just taking refs,

\( @a )

where the extra parens make the \ operator apply to a list rather than
an array. Passing a list to \ returns a list of refs to each member. So
the original can be replaced with

push @bindargs, \( @a );

but if @bindargs starts empty then we can forget the push and simply
initialize it right away

my @bindargs = \( @a );

more-or-less as you said :).

Ben
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top