Search code examples
perlmojolicious

Iterating through database results


I am returning data from my query. Looping through the results and pushing the data in different arrays. When I try to then loop over the array I am only getting the first set of data and nothing else.

 my @data = ();
 for my $row (@$query_data) {
     if($row->[11] == 2)
     {
          push(@data,$row)
     }
 }

On the perl script the following works and I am getting expected output.

    for my $row (@xm_data) {
       print "$row->[6]\n";
    }

outputs

   mike
   steve

When I do the samething more or less in mojolicious I am getting different results. I am passing to the render looks xm_data => @xm_data and only getting the first results.
The template loops is below.

% for my $request_data (@{$xm_data}) {
    <%= $request_data %>
% }

if i try the same syntax I do in the .pl file I get errors.


Solution

  • I don't know anything about mojolicious, but I do know some perl.

    Your xm_data => @xm_data looks suspicious to me. If you assign an array to a scalar in perl, it only takes the first element of that array. To assign a pointer to the array, which it looks like you're trying to do, you need to use a backslash.

    Eg. xm_data => \@xm_data.

    That will give you a reference to the array, which then you can dereference later in your foreach loop, like it seems you are doing already.

    Does that help?