How do you use multidimensional data structures with a short example?

In Perl, you can use multidimensional data structures such as arrays of arrays or hashes of hashes to organize complex data. Here is a simple example of each type:

Multidimensional Array Example

my @matrix = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] ); foreach my $row (@matrix) { foreach my $element (@$row) { print "$element "; } print "\n"; }

Multidimensional Hash Example

my %data = ( 'fruit' => { 'apple' => 1, 'banana' => 2 }, 'vegetable' => { 'carrot' => 3, 'potato' => 4 } ); foreach my $category (keys %data) { foreach my $item (keys %{ $data{$category} }) { print "$category: $item -> $data{$category}{$item}\n"; } }

Perl multidimensional arrays hashes data structures