On Aug 17, 5:07 pm, [EMAIL PROTECTED] wrote:
> I'm reading the book Programming the Perl DBI to start work with
> databases.  The DBI portion I'm getting OK, but I find the reference
> here to @$ confusing. (This code is based from the book.)
>
> my $sth1 = $dbh->prepare("select 'SCM_ORDERS_OUT', message_index,
> message_no, message_event, time_stamp, data from log");
>
> $sth1->execute();
>
> my @stash;
>
> while ($array_ref = $sth1->fetchrow_arrayref) {
>   push @stash, [ @$array_ref ];  ##copy array contents

Is that seriously from the book?  UGH.  Just declare $array_ref in the
proper scope, and there's no need to take a reference to a dereference
of the reference.

while (my $array_ref  = $sth1->fetchrow_arrayref) {
    push @stash, $array_ref;
}

>
> }
>
> ###dump stash contents
> foreach $array_ref ( @stash ) {
>    print "Row: @$array_ref\n";
>
> }
>
> The rows print out fine, so it "works", but I don't understand how.  I
> need to be able to work with specific items in the output, such as the
> column "data".  How would I reference it, using this example?

In this specific example, "data" is the sixth column in the SELECT
query.  You would therefore access it by $array_ref->[5].

I would generally recommend you use the fetchrrow_hashref method
rather than fetchrow_arrayref, so that you can refer to the column by
name.  This will prevent bugs in your code if you later change the
query to add or remove columns.  With fetchrow_hashref, the return
value is a reference to a hash rather than to an array:

while (my $hash_ref = $sth1->fetchrow_hashref()) {
    print $hash_ref->{data} . "\n";
}

I agree with other posters - you should read:
perldoc perlref
perldoc perlreftut
perldoc perllol
perldoc perldsc

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to