variable number of arguments to bind_columns
variable number of arguments to bind_columns
am 05.12.2007 13:58:56 von 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
Re: variable number of arguments to bind_columns
am 05.12.2007 15:01:01 von alexxx.magni
ok, dumb question (I must be tired)
....
push @bindargs,\$a[1];
....
push @bindargs,\$a[2];
....
$sth->bind_columns(undef,@bindargs);
alessandro
alexxx.magni@gmail.com ha scritto:
> 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
Re: variable number of arguments to bind_columns
am 05.12.2007 16:46:55 von Glenn Jackman
At 2007-12-05 09:01AM, "alexxx.magni@gmail.com" wrote:
> ok, dumb question (I must be tired)
> ...
> push @bindargs,\$a[1];
> ...
> push @bindargs,\$a[2];
> ...
> $sth->bind_columns(undef,@bindargs);
A more compact replacement for 'n' push commands is:
my @bindargs = map { \$_ } @a;
As in:
$ perl -MData::Dumper -e '@a=qw(a b c);@b=map{\$_}@a;print Dumper(\@a,\@b)'
$VAR1 = [
'a',
'b',
'c',
];
$VAR2 = [
\$VAR1->[0],
\$VAR1->[1],
\$VAR1->[2],
];
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: variable number of arguments to bind_columns
am 05.12.2007 17:35:58 von Ben Morrow
Quoth Glenn Jackman :
> At 2007-12-05 09:01AM, "alexxx.magni@gmail.com" wrote:
> > 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