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

Discussion in 'Perl Misc' started by minjie@excite.com, May 23, 2005.

  1. Guest

    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.
     
    , May 23, 2005
    #1
    1. Advertising

  2. wrote in
    news::

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 23, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , May 23, 2005
    #3
  4. wrote in news::

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 23, 2005
    #4
  5. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 24, 2005
    #5
  6. Guest

    --------------- 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.
     
    , May 24, 2005
    #6
  7. wrote in news::

    > 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.


    <URL: http://groups-beta.google.com/group/comp.lang.perl.misc/msg/f1284a41cce93112?dmode=source&hl=en>


    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 25, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiggaz
    Replies:
    6
    Views:
    32,182
    Raghudubba
    Nov 5, 2009
  2. helmut januschka

    warning: excess elements in scalar initializer

    helmut januschka, Mar 6, 2004, in forum: C Programming
    Replies:
    5
    Views:
    15,245
    Joe Wright
    Mar 6, 2004
  3. Clint Olsen
    Replies:
    6
    Views:
    378
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  4. Chuckb
    Replies:
    6
    Views:
    126
    David H. Adler
    Feb 10, 2004
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    171
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page