You only have one array here - well you have a second array variable, but you only ever store a single, scalar value in it.
Let's look at your code:
my $i;
my @carrierValuesAll="";
Declaring $i so far away from where it is used is a bit of a code smell. It'll work fine, of course, but Perl gives you the flexibility to declare variables where you use them - so use it.
And @carrierValuesAll="" isn't doing what you think it's doing. I suspect you think it's declaring an empty array. But actually it creates an array containing a single element which is an empty string.
while (my @row = $sth->fetchrow_array()) {
my @carrierValues=join(',', @row). "\n";
push(@carrierValuesAll, @carrierValues);
}
You now declare a new array, @carrierValues, but you give it a value which is returned from join(). The whole point of join() is to take a list of values (your data in @row) and join it together into a single string. You then push that single string onto the end of @carrierValuesAll.
There is no point in storing the result from join() in an array. The result from join() is only ever a single, scalar value.
So after this loop has finished running, you have an array, @carrierValuesAll which contains one of these single strings for each of the rows in your database table. There are no "subarrays" here. Just an array of strings.
You then confirm that by printing the contents of @carrierValuesAll.
$len = @carrierValuesAll;
for ($i = 0; $i < $len; $i = $i + 1) {
print ("\@carrierValuesAll[$i] = $carrierValuesAll[$i]\n");
print("\n");
}
And that prints exactly what I would expect. You get the data that you read from the database separated by commas.
If you don't want a single string, then don't use join() to make one. Instead, take a copy of @row and store a reference to that in your array:
while (my @row = $sth->fetchrow_array()) {
push(@carrierValuesAll, [ @row ]);
}
I'm not sure what you want to do with your array of arrays, but here's how you could print it out:
$len = @carrierValuesAll;
for (my $i = 0; $i < $len; $i = $i + 1) {
print ("\@carrierValuesAll[$i] = @{$carrierValuesAll[$i]}\n");
print("\n");
}
Note that I've used @{$carrierValuesAll[$i]} to dereference the array reference stored at $carrierValuesAll[$i] and turn it back into an array which I can then print.
Note also that I've moved the declaration of $i into the for loop where it makes more sense.
But most Perl programmers wouldn't use this "C-style" for loop as it looks horrible and is harder to understand. I would write this code as:
for my $i (0 .. $#carrierValuesAll) {
...
}
Instead of calculating the length of the array and then remembering to stop one iteration before reaching that, I've used the special variable $#ARRAY which gives me the index of the last index in the array.
my @carrierValues=join(',', @row). "\n";Why are you trying to assign a single scalar to an array?@carrierValuesAll. Just iterate usingforloop and get each element (See 'foreach loop and perl arrays' in this link). No need to have nested loop. To see the content of an array useData::Dumperhere which is very handful.