I use this method all the time at work, and dealing with 2-D arrays is a snap. The general rule is, for each dimension, you will need one for loop to iterate through the elements.
Let's say that you have just issued a database call and have the data stored in a reference. To make it easy, let's pretend that this bit of code is really that database call:
$db_results is really a 2-Dimensional array, each list points to a list (or list of lists: LoL). Now, here is a cool way to use this data:my $db_results = [ [qw(one two three)], [qw(four five six)], [qw(seven eight nine)], ];
This yeilds a nice HTML table:print "<table>\n"; foreach my $row (@$db_results) { print "\t<tr>\n"; foreach my $col (@$row) { print "\t\t<td>$col</td>\n"; } print "\t</tr>\n"; } print "</table>\n";
As you can see, arrays and loops go hand in hand. Two array, two loops - three arrays, three loops. Four starts getting hairly, and five is right out! Sorry, MP joke. :)<table> <tr> <td>one</td> <td>two</td> <td>three</td> </tr> <tr> <td>four</td> <td>five</td> <td>six</td> </tr> <tr> <td>seven</td> <td>eight</td> <td>nine</td> </tr> </table>
jeffa
In reply to (jeffa) Re: What is a multidimensional array and how do I use one
by jeffa
in thread What is a multidimensional array and how do I use one
by Hopeless
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |