The below code works if $a is an Array of Arrays, but I need $a to be an reference to the Array of Arrays.
Question
How do I iterate through $a?
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my @AoA = ( ['aaa','hdr','500'],
['bbb','jid','424'],
['ccc','rde','402'],
);
my $a = \@AoA;
my $s = "bbb";
my $d = "ddd";
for my $i ( 0 .. $#a ) {
for my $j ( 0 .. $#{ $a[$i] } ) {
if ($a[$i][$j] eq $s) {
$a[$i][$j] = $d;
last;
}
}
}
print Dumper $a;
Also, to compute the # of elements in array reference (which as you can see from above code you don’t need for your specific code), the easiest approach is:
Also, to access the data inside an arrayref of arrayrefs, you simply use the dereference operator on it:
In addition, you can create your arrayref of arrayrefs the way you did (take a reference to array of arrays), or right away as a reference:
As a side note, please never use
$aand$bas identifyer names – they have special purpose (used in sort blocks for example)