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 same 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?