how to "spell out" the elements in a scalar variable?

M

minjie

Hello,

I'm using DBI to retrieve data from a DB2 database for each column in a
table (for a web application). The columns are not statically declared
beforehand but composed dynamically into a scalar variable
$cols_for_bind, using the following sub routine:

sub columns_for_binding
{
my ($arr_ref, $prefix) = @_;
foreach (@{$arr_ref})
{
#print "in columns_for_binding func: $_\n\n";
$cols_for_bind .= "\\\$" . "$prefix" . $_ . ", ";
}
$cols_for_bind = substr($cols_for_bind, 0, length($cols_for_bind) -
2);
}

$arr_ref contains a list of column names for a table that I retrieved
from the database, and after executing the above routine, the value in
$cols_for_bind can be as follows, for example:

$col_for_bind = "\$mod_num, \$inst_num, \$param_name, \$from_value,
\$to_value, \$modified_date";

and I used the following statement to bind the columns:

$sth->bind_columns( undef, eval{$cols_for_bind});

I was hoping to "spell out" the 6 columns before bind_columns()
function is actually executed. But I kept getting the following error:

[Mon May 23 15:40:27 2005] [error] [client xx.x.x.xxx] bind_columns
called with 1 refs when 6 needed. at
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/DBI.pm line
1705.

so bind_columns() still treats eval{$cols_for_bind} as one parameter.
How should I go about for what I intend to do?

Thanks for any help.
 
A

A. Sinan Unur

(e-mail address removed) wrote in
I'm using DBI to retrieve data from a DB2 database for each column in
a table (for a web application). The columns are not statically
declared beforehand but composed dynamically into a scalar variable
$cols_for_bind, using the following sub routine:

sub columns_for_binding
{
my ($arr_ref, $prefix) = @_;
foreach (@{$arr_ref})
{
#print "in columns_for_binding func: $_\n\n";
$cols_for_bind .= "\\\$" . "$prefix" . $_ . ", ";
}
$cols_for_bind = substr($cols_for_bind, 0, length($cols_for_bind)
-
2);
}

$arr_ref contains a list of column names for a table that I retrieved
from the database, and after executing the above routine, the value in
$cols_for_bind can be as follows, for example:

$col_for_bind = "\$mod_num, \$inst_num, \$param_name, \$from_value,
\$to_value, \$modified_date";

and I used the following statement to bind the columns:

$sth->bind_columns( undef, eval{$cols_for_bind});

I was hoping to "spell out" the 6 columns before bind_columns()
function is actually executed. But I kept getting the following error:

[Mon May 23 15:40:27 2005] [error] [client xx.x.x.xxx] bind_columns
called with 1 refs when 6 needed. at
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/DBI.pm line
1705.

so bind_columns() still treats eval{$cols_for_bind} as one parameter.
How should I go about for what I intend to do?

I am not sure what you intend to do because your explanation is fairly
convoluted.

Here is the documentation for bind_columns:

bind_columns

$rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);

Calls bind_col for each column of the SELECT statement. The
bind_columns method will die if the number of references does
not match the number of fields.

For maximum portability between drivers, bind_columns() should be
called after execute() and not before.

From above:
and I used the following statement to bind the columns:

$sth->bind_columns( undef, eval{$cols_for_bind});

What do you think the first undef should do?

Why not put each of the references in an array, and use that?

Please do review the posting guidelines for information on how to help
yourself, and help others help you.

Sinan
 
M

minjie

Sorry for the vague wording. I did not mean the execute() function, the
"execute" I wrote was purely an English word meaning I want to expand
the $cols_for_bind to 6 references to variables before (or to be more
precise I should say 'while', not 'before') bind_columns() function
proceeds (executes), such as the example below:
$sth->bind_columns( undef, \$mod_num, \$inst_num, \$param_name,
\$from_value, \$to_value, \$modified_date);
I know the above statement works, but my problem is that I have to do
it more dynamically than that. I did not know one can bind columns to
array elements. Thanks for the info.
 
A

A. Sinan Unur

Sorry for the vague wording.

[ Which wording? Please quote an appropriate amount of context when you
reply. See
<URL: http://groups-beta.google.com/group/comp.lang.perl.misc/msg/f1284a41cce93112?dmode=source&hl=en>
for more information.
]
$sth->bind_columns( undef, \$mod_num, \$inst_num, \$param_name,
\$from_value, \$to_value, \$modified_date);
I know the above statement works, but my problem is that I have to do
it more dynamically than that. I did not know one can bind columns to
array elements.

Ahem, are you looking for:

perldoc -f push

It seems, you skipped

perldoc perlfunc

when you read all the documentation that was installed along with
your copy of Perl.
Thanks for the info.

You can show your gratitude by reading, and trying to follow the
posting guidelines for this group. This is the second time I am
having to point this out.

Sinan
 
X

xhoster

Hello,

I'm using DBI to retrieve data from a DB2 database for each column in a
table (for a web application). The columns are not statically declared
beforehand but composed dynamically

Then use a hash for God's sake.

snip
$arr_ref contains a list of column names for a table that I retrieved
from the database, and after executing the above routine, the value in
$cols_for_bind can be as follows, for example:

$col_for_bind = "\$mod_num, \$inst_num, \$param_name, \$from_value,
\$to_value, \$modified_date";

The value assigned to $col_for_bind by the above code is not the same
as would be assigned to it by the code in the loop (which I snipped).
and I used the following statement to bind the columns:

$sth->bind_columns( undef, eval{$cols_for_bind});

This is a block eval. You are looking for string eval. (Although
really you should be looking for hashes, and stop all this symbolic
reference stuff)

Xho
 
M

minjie

--------------- the last part of your reply ------
This is a block eval. You are looking for string eval. (Although
really you should be looking for hashes, and stop all this symbolic
reference stuff)
----------------------------------------------------------

Hello Xho, when I clicked on the Reply link under your message, I did
not get any of your original message. So I pasted some (that I'm
replying to) above.

Thanks a lot for your advice! I'm new to Perl (so I'm not familiar with
hash and such) and I'm modifying someone else's existing code, which
was written several years ago, so I was just trying to following that
person's logic and modify the code as little as possible. But I agree
with you that playing around with symbolic reference stuff is not a
good way at all. I'll need to use the proper data structures, such as
hash, HoL, etc.
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top