Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: array references in DBI

by ioannis (Abbot)
on May 13, 2006 at 09:48 UTC ( [id://549207]=note: print w/replies, xml ) Need Help??


in reply to array references in DBI

Note. The code that worked came close to not working either. Each iteration tried to save the same reference!
push @OrderedQuestions, \@an ;
It only worked because @an was declared lexical and not global:

This almost identical code will not work
with the keyword my is missing:

while ( @an = $sth->fetchrow_array) { push (@OrderedQuestions, \@an); }

Replies are listed 'Best First'.
Re^2: array references in DBI
by BrianC (Acolyte) on May 13, 2006 at 16:14 UTC
    Thank you for pointing this out. What if @an was lexical but declared before the while loop? I would have the same problem, no?
      Yes, you would be pushing the same array ref every time, and the contents would change afterwards.

      Test code (no DBI involved):

      my @rows; my @row = qw(one two); push @rows, \@row; @row = qw(three four); use Data::Dumper; print Dumper \@rows; __END__ $VAR1 = [ [ 'three', 'four' ] ];

      As you can see, the contents of the collating array changes after you put it in there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://549207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-06-23 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.