• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Need Perl 2d Array help?

Need to create a 2d array in perl and I'm having one of those days where nothing I find online is making any sense to me. I wrote some fake code below to give you the flow and what i'm wanting to do. Any idea how to actually do this? Just need an example of a 2d array that consist of 3 columns (each an array). The array will get filled with a push in the loop that goes through each of the database row results.

Thanks! Let me know if there is any extra information I can use? Or if there is a better way to do this? For the DB query I'm using the DB Oracle something module.
my @array = (@columns1, @columns2, @columns3)
my @columns1;
my @columns2;
my @columns3;
#In the real code a query is run here and returns results w/ 3 columns and so many rows.

while( $query_results->fetch() ){
#Need to load up the multidimensial array here w/ the query results for each of the columns.

}  

#Then print here. But really I will be looping through the array and doing all kinds of stuff. But a loop example w/ printing the 2d array will give me enough info to get going.

Open in new window

0
KGNickl
Asked:
KGNickl
  • 2
2 Solutions
 
Carl BohmanCommented:
Line 1 will just combine all of the arrays into a single array.  That's not what you want in this case.

Here's a few ways to create a two-dimensional array:

This links the variables together, so if you change a value in one variable, the corresponding value in the referenced variable also changes:
my @array = (\@columns1, \@columns2, \@columns3);

This creates new copies of the values in the columns arrays:
my @array = ([@columns1], [@columns2], [@columns3]);

To loop over a two-dimensional array, you need two nested loops:
for (my $row = 0; $row < @array; $row++)
{
  for (my $col = 0; $col < @{$array[$row]}; $col++)
  {
    ...do something with $array[$row][$col]...
  }
}
0
 
Carl BohmanCommented:
One thing to keep in mind (and the reason why I didn't specifically loop to 3 for $col), in Perl, there's no constraint that says that each array will be the same length when you have a multi-dimensional array.  Perl lets each be completely independent.  You can make your code ensure a specific length if you want to.  Database query code should have a reliable length as long as the query is structured correctly and valid results are being returned.
0
 
wilcoxonCommented:
Based on your description, it's unclear - do you want an array of arrays of rows of 3 values or an array of arrays of column data (with however many entries there are for each column)?  Your example indicates array of arrays of column data but your text made me wonder if you really wanted the other.

If you want array of arrays of column data, bounsy's answer should at least give you idea.

my @columns = ([], [], []);

#In the real code a query is run here and returns results w/ 3 columns and so many rows.

while( my $row = $query_results->fetchrow_arrayref ){
    for my $i (0..2) {
        # to keep null/undef values, remove the "if defined($row->[$i])"
        push @{$columns[$i]}, $row->[$i] if defined($row->[$i]);
    }
}  

#Then print here. But really I will be looping through the array and doing all kinds of stuff. But a loop example w/ printing the 2d array will give me enough info to get going.
foreach my $i (0..@columns-1) {
    foreach my $j (0..@{$columns[$i]}-1) {
        print "$i/$j = $columns[$i][$j]\n";
    }
}

Open in new window


If you want array of array of rows of 3 values this should work...

 
my @array;

#In the real code a query is run here and returns results w/ 3 columns and so many rows.

while ( my $row = $query_results->fetchrow_arrayref ){
    push @array, $row;
}  

#Then print here. But really I will be looping through the array and doing all kinds of stuff. But a loop example w/ printing the 2d array will give me enough info to get going.

foreach my $row (@array) {
    print "row = ", join(', ', @$row), "\n";
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now