If you really want to stick to using an array of hashes, then this will work for you.
The for loop iterates over all the indices of @test and @test2. I've used the min function from List::Util to find the last index of the shorter of the two arrays. This avoids falling off the end of one or other of the arrays in case they are different lengths.
The highest-numbered hash key is found by using a regular expression to extract the numeric part of each key, together with the max function, also from List::Util. The next key to be used is one more than this maximum, preceded by the string VAR.
I've used Data::Dump only to show the resultant data structure.
use strict;
use warnings;
use List::Util qw/ min max /;
my @test = (
{ VAR1 => 1, VAR2 => 2, VAR3 => 3 },
{ VAR1 => 11, VAR2 => 22, VAR3 => 33 },
{ VAR1 => 111, VAR2 => 222, VAR3 => 333 },
);
my @test2 = (4, 44, 444);
my $limit = min $#test, $#test2;
for my $i (0 .. $limit) {
my $max = max map /(\d+)/, keys %{ $test[$i] };
$test[$i]{'VAR'.++$max} = $test2[$i];
}
use Data::Dump;
dd \@test;
output
[
{ VAR1 => 1, VAR2 => 2, VAR3 => 3, VAR4 => 4 },
{ VAR1 => 11, VAR2 => 22, VAR3 => 33, VAR4 => 44 },
{ VAR1 => 111, VAR2 => 222, VAR3 => 333, VAR4 => 444 },
]
But if you want to go for an array of arrays, then it will look more like this. It's quite similar, except that I can just use push instead of calculating an index for the next element of the arrays.
use strict;
use warnings;
use List::Util qw/ min /;
my @test = (
[ 1, 2, 3 ],
[ 11, 22, 33 ],
[ 111, 222, 333 ],
);
my @test2 = (4, 44, 444);
my $limit = min $#test, $#test2;
for my $i (0 .. $limit) {
push @{ $test[$i] }, $test2[$i];
}
use Data::Dump;
dd \@test;
output
[[1 .. 4], [11, 22, 33, 44], [111, 222, 333, 444]]
@test2 = (4, 44, 444)? As you've written it it's a one-element array. Are your hash keys reallyVAR1,VAR2etc? Because if so then you need arrays and not hashes.