in reply to Re: DBI fetchall_arrayref
in thread DBI fetchall_arrayref

OK the code below works. the de-referencing is tricky

$sqlStr = "select createwbparserun( '$directory' , '$filename' ) " +; print $sqlStr. "\n" ; my $sth = $db_in->prepare( $sqlStr ); $sth->execute(); my $aryRef = $sth->fetchall_arrayref([0]); my $a = $ {$aryRef }[0]; print "Reference: $aryRef\n"; print "Dereferenced: $a\n"; print "Deref again: ${$a}[0] \n"; $wb_runkey = ${$a}[0]; print $aryRef->[0]->[0] . "\n"; ##print Dumper @a; ##$wb_runkey = $a[0]; print "WORKBOOK run key: " . $wb_runkey . "\n" ;
it produces the following:

select createwbparserun( 'C:/dev/HN_optimum/' , 'FM Mk 14 - 60M200007_ +DB200609.xls' ) Reference: ARRAY(0x34dfed8) Dereferenced: ARRAY(0xd5079a8) Deref again: 55 55 WORKBOOK run key: 55 Died at parseExcelWB.pl line 198.
Here is a link the explains the de-referencing: http://www.thegeekstuff.com/2010/06/perl-array-reference-examples/

thanks for your kind assistance.

KD

Replies are listed 'Best First'.
Re^3: DBI fetchall_arrayref
by NetWallah (Canon) on Dec 17, 2011 at 04:51 UTC
    Your call to fetchall_arrayref returns ALL ROWS.

    So, your first dereference gets you an array of Rows (Your array has only one element).

    Now, you need to dereference again to get to the value stored within column(0) of that row.

    You can accomplish all this in a single step:

    my $wb_runkey = $aryRef->[0]->[0]; # Can also be written as : # $aryRef->[0][0]; # Arrow in between is implied # ${$aryRef}[0][0]; # Look ma - no arrows (But this is uglier, IM +HO)
    Hopefully, this exercise dipped your toe into dereferencing - an extremely powerful construct, for advancing with perl.

                "XML is like violence: if it doesn't solve your problem, use more."

Re^3: DBI fetchall_arrayref
by TJPride (Pilgrim) on Dec 17, 2011 at 03:26 UTC
    Don't really need to fetchall if you're only accessing the first record. And you're probably going to find a hash a lot simpler to work with in most circumstances. What do you get if you do $sth->fetchrow_hashref() ?