Thanks to all for the further discussion on this. After this tonight, going
back to my original question about reusable code for binding columns, this
makes for a slicker solution than I even though would come from asking the
question (and I know some of you are slick)  :-)

That would mean in my subroutine, I can do something like (shorthand
version, but still critique it if it needs it):

        my $select = qq{SELECT $columnlist FROM $ini->{sourcedb}..$table};
        my $columns = $columnlist;

        # Allow for the possibility of non-standard column names

          $columns =~ s/ //;
          $columns =~ s/,/ /;
        my $selprep = $source->prepare($select) || print logfile "Can't prepare:
$select \n";
          $selprep->execute() || print logfile "Can't Execute $select \n";

        # and the solution to my original question:

          $rc = $sth->bind_columns(\@column{qw($columns)});


And be able to refer to any number of columns by names still recognizable as
the original column name (minus any spaces that might have been in the
source), and still even have the original column list in tact to work with
later.

Cool!  That's even better than I exected as a solution.

Thanks a lot to all of you who responded, and contributed.

Steve Howard

-----Original Message-----
From: James Maes [mailto:[EMAIL PROTECTED]]
Sent: Friday, March 09, 2001 11:28 PM
To: Sterin, Ilya; Matthew O. Persico; [EMAIL PROTECTED]
Subject: Re: Reusable code for binding columns.




still need an ='s sign for anonymouse arrays

@hash = {'hi_there','bye_there','over_there'};
$hash{'bye_there'} = "testing";
print $hash{'bye_there'};


On Friday 09 March 2001 23:10, Sterin, Ilya wrote:
> That's a good explanation, since I myself understood everything except the
> @ usage for this expression.  Now I can see, but why doesn't this work
with
> my perl5.6
>
> use strict;
> my @hash{'hi_there','bye_there','over_there'};
> $hash{'bye_there'} = "testing";
> print $hash{'bye_there'};
>
> Any ideas, it comes with...
> syntax error at test.pl line 2, near "@hash{"
> Execution of test.pl aborted due to compilation errors.
>
> -----Original Message-----
> From: Matthew O. Persico [mailto:[EMAIL PROTECTED]]
> Sent: Friday, March 09, 2001 11:56 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Reusable code for binding columns.
>
> "Thomas A. Lowery" wrote:
> > OK Randal, how does this work?  I put it in code and see it WORKS, but
my
> > brains hurts trying to understand it.
> >
> > > $rc = $sth->bind_columns(\@column{qw(one two three four five)});
>
> Step back a bit.
>
> An array element is $array[0].
>
> An array slice is @array[1,2,3,4], for example.
>
> A hash element is $hash{'hi_there'}. I know you don't NEED the quotes,
> but they make sense for the purposes of the demo.
>
> A hash slice is @hash{'hi_there','bye_there','over_there'} for example.
>
> <aside>
> As I understand it the reason that a HASH slice is defined with an ARRAY
> indicator (@) is that in this context, @ is NOT an array indicator -
> it's a LIST indicator. The type of list is defined by the brackets; []
> is an array, a list of elements, {} is a hash, a list of paired
> elements.
> </aside>
>
> Therefore, @column{'one', 'two', 'three', 'four', 'five'} is a hash
> slice, which is simply a list of the values at these keys of the hash
> %column.
>
> Typing all the quotes is a PITA, so we use the "quote word" operator to
> simplify things to
>
> @column{qw(one two three four five)}
>
> Now, according to the docs (http://www.perldoc.com/cpan/DBI.html)
>
>       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.
>
> So, in order to get a list_of_refs, you put \ before the list. This does
> NOT create a reference to the whole list, as you'd might expect. Rather
> the "ref"-ness gets distributed to each element of the list, creating a
> list of REFs.
>
> Personally, I think this is a perfect example of the "beauty" of Perl.
> Visually, \@column{qw(one two three four five)} LOOKS like a ref of a
> list. Bit, I don't think there IS such a thing.
>
> \@foobar IS a ref of an ARRAY.
> \%baba is a ref of a HASH.
> \(1,2,3,4) is a ref of a LIST. But what's the point of that? If you want
> an ARRAY ref, you can do [1,2,3,4]. If you want a hash ref, do
> \{1,2,3,4} (or, more clearly, \{1=>2,3=>4}). A ref of a LIST is
> ambiguous, as used in this context. Therefore, it can't be used that
> way. So, I guess, it was decided that defining this construct as
> converting a list to a list of refs would be useful and therefore, it
> was done.
>
> <shameless flame-bait>
> Now, IMHO, the majority of compsci language police would end up in
> straight jackets if they tried to create a BNF for this scenario. Tough.
> Perl gets the job done w/o slavish devotion to an academic concept. Let
> Gossling, and Guido chew on that! harumph! Perl "just makes sense". It's
> as close to a DWIM language as we have today.
> </shameless flame-bait>
>
> HTH
>
> Oh, and by the way, just because I think I can explain it, don''t for
> one momment belive that I'd EVER dream that one up on my own. :-)
>
> But you shouldn't be surprised that Randal did. Remember, he's the
> author of the Schwarzian transform.
>
> --
> Matthew O. Persico
>
> http://www.acecape.com/dsl
> AceDSL:The best ADSL in Verizon area

--

------------------------------------
|| James Maes
|| Senior Programmer
|| [EMAIL PROTECTED]
|| The Sporting News
|| www.sportingnews.com
|| fantasy.sportingnews.com
------------------------------------

Reply via email to