0

I have created one array and pushing another array in each iteration of loop into it. Once it will be done I wanted to fetch the values of each sub array as well as array.

Code I am using :

my $i;
my @carrierValuesAll="";

while (my @row = $sth->fetchrow_array()) {

    my @carrierValues=join(',', @row). "\n";
    push(@carrierValuesAll, @carrierValues);
}

$len = @carrierValuesAll;
for ($i = 0; $i < $len; $i = $i + 1) {
    print ("\@carrierValuesAll[$i] = $carrierValuesAll[$i]\n");
    print("\n");    
}

The output I am getting is :

@carrierValuesAll[1] = 1,1,https://au-sbc.trustidinc.com/tid,sbcLabStub,sbcLab,,SKY,0,2019-11-07 20:10:43,2021-07-02 04:39:43,TrustID Lab Oracle,Y,Y,Y,ivr.localdomain,Y,trustid

@carrierValuesAll[2] = 2,1,https://au-sbc.trustidinc.com/tid,sbcLab,sbcLab,,SKY,2,2019-11-07 20:10:43,2020-12-14 06:24:17,TrustID Lab Oracle,Y,Y,Y,ivr.localdomain,Y,admin

What I have tried :

$len = @carrierValuesAll;
for ($i = 0; $i < $len; $i = $i + 1) {
    print ("\@carrierValuesAll[$i] = $carrierValuesAll[$i]\n");
    print("\n");    
    for (my $j=0;$j<$carrierValuesAll[$i];$j++) {
        print("\@carrierValuesAll[$j] = $carrierValuesAll[$j]\n");
        print("\n");
    }
}

added one more nested loop into it, but it wont helped.

This output is what coming from database & I am trying to store it in array.

4
  • 1
    my @carrierValues=join(',', @row). "\n"; Why are you trying to assign a single scalar to an array? Commented Jun 1, 2022 at 8:12
  • 2
    Right now you have everything in your array @carrierValuesAll. Just iterate using for loop 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 use Data::Dumper here which is very handful. Commented Jun 1, 2022 at 8:17
  • I think you may have forgotten to indicate what your problem is. I see you present code (somewhat iffy code, but whatever), print the output that looks fine, and then say you're trying to store it an array. Nothing indicates a problem. Commented Jun 1, 2022 at 15:26
  • 1
    metacpan.org/pod/DBI#fetchall_arrayref Commented Jun 1, 2022 at 15:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Can't use string ("") as an ARRAY ref while "strict refs" in use at ./aa.pl line 110. it is giving me this error and line 110 is print ("\@carrierValuesAll[$i] = @{$carrierValuesAll[$i]}\n"); this line
@LalitKumarSingh: We're not here to hand you answers on a plate. I explained the problem with my @carrierValuesAll="". Just replace it with my @carrierValuesAll (or my @carrierValuesAll=() if you're obsessive about initialising variables.
Actually this solves my problem as well as this taught me about scalars and arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.