or$anon_array_ref = ['scalar_1', 'scalar_2', 'scalar_3'];
At the end of this chapter a small discussion on using a reference to an Array of Arrays.$anon_array_ref = [ qw( scalar_1 scalar_2 scalar_3 ) ];
or@AoA = ( # array of arrays; notice the open and close parenthesis: it's a real array: a list assigned to an array variabele!
['red', 'orange', 'yellow'],# first row with an anonymous array
['green', 'blue'],# second row with an anonymous array
);
To extend an Array of Arrays, use 'push':@AoA = ( [ qw( red orange yellow ) ], [ qw( green blue ) ], );
Creating an Array of Arrays can alternatively be done with successive push commands:push (@AoA, ['purple', 'pink', 'brown']); # add element to @AoA
You can of course convert strings, read e.g. from a text file, and push them to an AoA:@AoA = (); # empty the Array of Arrays
push (@AoA, ['red', 'orange', 'yellow']);
push (@AoA, ['green', 'blue']);
push (@AoA, ['purple', 'pink', 'brown']);
@AoA = (); # empty the Array of Arrays
$str = "red, green, blue";
@split = split(', ', $str);
push @AoA, [ @split ];
@AoA_copy = @AoA;
@AoA_combi = (@AoA_1, @AoA_2);
@AoA = ();
@AoA = (
['red', 'orange', 'yellow'],# first row
['green', 'blue'],# second row
);
print "$AoA[0][1]\n";# access element via indices [0][1]: first row, second column; output: orange
$length_AoA = scalar(@AoA);
$index_last_element_AoA = $length_AoA - 1;
$index_last_element = $#AoA; # the operator '$#' returns the last index of an array and also of an AoA
The 'foreach' loop can be shorter:foreach $i (0 .. $#AoA) { # $#AoA is index last element
print "Array $i: @{@AoA[$i]}\n";
}
or (relying on the special variable $_):foreach $aref (@AoA) {
print "[ @{$aref} ]\n";
}
foreach (@AoA) {
print "[ @{$_} ]\n";
}
@AoA_copy = @AoA;
foreach (@AoA_copy) {
print "[ @{$_} ]\n";
}
Use 'unshift' for adding an array at the beginning:push (@AoA, ['purple', 'pink', 'brown']);
The functions 'shift' and 'pop' can be used for respectively removing an array at the end or at the beginning.unshift (@AoA, ['purple', 'pink', 'brown']);
pop (@AoA);
shift (@AoA);
$AoA[2][1] = 'PINK'
@AoA = (
['red', 'orange', 'yellow'],# first row
['green', 'blue'],# second row
['purple', 'pink', 'brown'],# third row
);
@AoA_copy = @AoA;
splice (@AoA_copy, 0, 1);# remove the first member, i.e. first array from @AoA_copy
@AoA_copy = @AoA;
splice (@AoA_copy, 0, 2);# remove the first two members from @AoA_copy
The best way to remove an existing array from the Array of Arrays conditionally is to store matches in a temporary Array of Arrays (AoA_temp). At the end you could copy this AoA_temp to the original AoA:@AoA_copy = @AoA;
splice (@AoA_copy, -1, 1);# remove the last member from @AoA_copy
@AoA = (
['red', 'orange', 'yellow'],# first row
['green', 'blue'],# second row
['black', 'white'],# third row
);
@AoA_temp = ();
foreach my $i (0 .. $#AoA) {
if (! grep (/blue/, @{$AoA[$i]} )) {
push (@AoA_temp, $AoA[$i]);
}
}
@AoA = ();
@AoA = @AoA_temp;
undef @AoA_temp;# forget @AoA_temp ever existed
foreach $aref (@AoA) {
print "[ @{$aref} ]\n";# output: [ red orange yellow ] and [ black white ]
}
@AoA_copy = @AoA;
foreach my $i (0 .. $#AoA) {
if (grep {'green' eq $_} @{$AoA_copy[$i]} and grep {'blue' eq $_} @{$AoA_copy[$i]}) {
print "[ $AoA_copy[$i][0] $AoA_copy[$i][1] ]\n";
}
}
If you want to work with a reference to an Array of Arrays, you've to deal with only some more derefencing operators. Be warned! Things can be a bit complicated.
@AoA returns two references (i.e. internal Perl key) as you'll see if you do 'print ("@A0A");'. In my case:@AoA = ( # array of arrays; notice the open and close parenthesis: it's a real array!
['red', 'orange', 'yellow'],# first row
['green', 'blue'],# second row
);
Let's go further:ARRAY(0x5564530d04b8) ARRAY(0x5564530d0548)
or$AoA_ref = \@AoA; # reference to an Array of Arrays
print "1: $$AoA_ref[1][1]\n";# access element via index row and column; alternative: $AoA_ref->[1]->[1]
push (@{$AoA_ref}, ['black', 'grey', 'white']);# add element to @AoA via reference
print "2: @{@$AoA_ref[2]}\n";# print added array member
$$AoA_ref[1][1] = 'BLUE';# change value of specific element # print Array of Arrays:
foreach $i (0..$#$AoA_ref) {
print "[ @{@$AoA_ref[$i]} ]\n";
}
orforeach $aref (@{$AoA_ref}) {
print "[ @{$aref} ]\n";
}
You see: it's all logical but not easier to understand...sometimes extra curly braces may help. Maybe useful for you:foreach (@{$AoA_ref}) {
print "[ @{$_} ]\n";
}
or$number_ref_arrays = scalar(@{$AoA_ref}); # output: 3
$last_index_ref_arrays = $#$AoA_ref;# output: 2
$last_index_ref_arrays = $#{$AoA_ref};# with extra curly braces; output: 2
$no_elements_ref_array = scalar(@{ $AoA_ref->[1] });# output: 2
$no_elements_ref_array = scalar(@{ $$AoA_ref[1] }); # without arrow and with extra $; output also: 2