Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3241968
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:15:36+00:00 2026-05-17T18:15:36+00:00

Question: Why can’t I push elements into a 2d array that’s inside of a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:15:37+00:00Added an answer on May 17, 2026 at 6:15 pm

    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]);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.