开发者

Perl - Push into arrays using variable references versus using variable copies

开发者 https://www.devze.com 2023-01-19 16:12 出处:网络
Question:Why can\'t I push elements into a 2d array that\'s inside of a while loop that parses through a SQL result set?

Question: Why can't I push elements into a 2d array that's inside of a while loop that parses through a SQL result set?

I need some help here as to why this happens. The way data is stored into a 2d array can be done several ways, but for my purposes, I need to use the push method.

Here's some code that works:

my @tt = (0,1,2,3);
my @t;
push (@t,\@tt);
print "[0][0]:".$t[0][0]."\n";
print "[0][1]:".$t[0][1]."\n";
print "[0][2]:".$t[0][2]."\n";
print "[0][3]:".$t[0][3]."\n";
@tt = (4,5,6,7);
push (@t,\@tt);
print "[1][0]:".$t[1][0]."\n";
print "[1][1]:".$t[1][1]."\n";
print "[1][2]:".$t[1][2]."\n";
print "[1][3]:".$t[1][3]."\n";

The output is:

-------------------------
[0][0]:0
[0][1]:1
[0][2]:2
[0][3]:3
[1][0]:4
[1][1]:5
[1][2]:6
[1][3]:7


Here's the problem I'm having

I'm running some SQL to build up a result set with X number of columns. To store the data into my array, I figured I could use the sam开发者_StackOverflowe syntax as above:

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

I tested the SQL and checked the result set, so the problem is not related to that. I have also reverted back to the long-hand approach which leaves me with my desired end result. Instead of using push(@stu_pool,\@results); I used this code inside the loop:

$stu_pool[$index][0] = $results[0];
$stu_pool[$index][1] = $results[1];
$stu_pool[$index][2] = $results[2];
$stu_pool[$index][3] = $results[3];
$stu_pool[$index][4] = $results[4];
$stu_pool[$index][5] = $results[5];
$stu_pool[$index][6] = $results[6];
$index++;

So what is preventing me from pushing elements into this array? It works fine with the first example, but when I try to print out the elements, they are all blank. The code I'm using:

print "[0][0]:".$stu_pool[0][0]."\n";
print "[0][1]:".$stu_pool[0][1]."\n";
print "[0][2]:".$stu_pool[0][2]."\n";
print "[0][3]:".$stu_pool[0][3]."\n";
print "[1][0]:".$stu_pool[1][0]."\n";
print "[1][1]:".$stu_pool[1][1]."\n";
print "[1][2]:".$stu_pool[1][2]."\n";
print "[1][3]:".$stu_pool[1][3]."\n";

To repeat:

Why does the push method fail to work inside the while loop?


Let me change your first example a bit.

my @tt = (0,1,2,3);
my @t;
push (@t,\@tt);
@tt = (4,5,6,7);
push (@t,\@tt);
print "[0][0]:".$t[0][0]."\n";
print "[0][1]:".$t[0][1]."\n";
print "[0][2]:".$t[0][2]."\n";
print "[0][3]:".$t[0][3]."\n";
print "[1][0]:".$t[1][0]."\n";
print "[1][1]:".$t[1][1]."\n";
print "[1][2]:".$t[1][2]."\n";
print "[1][3]:".$t[1][3]."\n";

Output:

[0][0]:4
[0][1]:5
[0][2]:6
[0][3]:7
[1][0]:4
[1][1]:5
[1][2]:6
[1][3]:7

You're not copying @tt into @t. You're putting a reference to the variable @tt into @t. So when something else happens to @tt, it changes when fetched through @t as well. To really make a copy, you want push(@t,[@tt]) or @{$t[$i]} = @tt if $i is an appropriate index variable.

Or for the other example, try push(@stu_pool, [@results]);


There's only one @results -- you're making every element of @stu_pool a reference to the same @results, which (the last time the loop condition is checked) will eventually be set to contain ().

What you want is to add my: while (my @results = $sth->fetchrow_array) { will work just fine, because now each time through the loop, @results is a different array.


Why loop at all? It's much easier to do:

my $results = $sth->fetchall_arrayref();

or if you insist on using an array variable:

my @results = @{$sth->fetchall_arrayref()};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号