Lewisa has asked for the wisdom of the Perl Monks concerning the following question:

I'm using Perl with MySql. I have query that I'm using with fetchall_arrayref to grab results data from my survey application. As recommended by documentation I use foreach to loop through fetchall_arrayref. Inside of this loop, I loop through my $answer1 data and run a counter to count all of the answers for a question and I want the total. When I push this information into an array and try to grab the last element in that array, I don't get the desired output. I ran a test outside of my fetchall_arrayref foreach loop, using the same logic and it was successful in getting me what I needed.

Here's my code:

my $testQuery = "SELECT questionNum, question, answer1 FROM results WH +ERE title = ? ORDER BY questionNum"; my $sty = $dbh->prepare($testQuery); $sty->execute($marathon); my $potential = $sty->fetchall_arrayref(); $sty->finish; my $previous_question; my $previous_answer; my $countEm; my @total; my @totalAnswer; my @norm; my $last_arr_index; foreach my $data (@$potential) { my ($questionNumber, $question, $answer1) = @$data; $answeredOne = 0; print qq{<tr><td>$questionNumber. $question</td></tr>} unless $pre +vious_question eq $question; if ($answer1 ne "" && $questionNumber == 1){ $optionOne = $answer1; $answeredOne = $answeredOne + 1; $countEm++; push @total, $countEm; push @totalAnswer, $optionOne; } if ($answeredOne != 0){ #my $elementCount = scalar(@total); #say $elementCount; say @total[-1]; } $previous_question = $question; $previous_answer = @totalAnswer[2]; }#end foreach
When I print: say @total[-1]; gives me the output of "1 2 3" when I just need "3".

I tested this same logic outside of the fetchall_arrayref foreach loop and I got the desired output:

my $counting; my @totalCounting; my @link = ('water', 'water', 'water', 'water', 'water'); foreach my $i (@link){ $counting++; push @total``Counting, $counting; } say @totalCounting[-1];
say totalCounting[-1]; gives me "5".

Why won't this work inside of fetchall_arrayref?

Replies are listed 'Best First'.
Re: How can I get the last element of an array inside of fetchall_arrayref?
by hippo (Archbishop) on Mar 23, 2021 at 17:47 UTC

    Your 2 examples have slightly different logic. In your first example (with the database) you are pushing and printing inside the loop. In your second example you are pushing inside the loop but printing outside it. This explains the difference in the outputs you are seeing.


    🦛

      Hey Hippo. Thank you! You are correct, I missed that error on my end. However, how can I print inside of the fetchall_arrayref loop and still get the last element, 3? All of my data is connected within that loop and the second I leave the loop they're no longer connected, correct?

        Your variables (scalars and arrays in this case) which are declared outside the loop will continue to hold their values once the loop is finished - just like they do in your second example. Try it and see!

        Scoping is an important topic in programming generally, not just in Perl. It will be worth your time to read and learn more about it. The usual reference given is Coping with scoping which covers lots more than you will need for this particular question - but it is all good stuff.


        🦛

Re: How can I get the last element of an array inside of fetchall_arrayref?
by choroba (Cardinal) on Mar 23, 2021 at 18:05 UTC
    Not related to your question, but: do you use warnings? They should have told you
    Scalar value @total[-1] better written as $total[-1] at ...

    If you know you only want one element from the array, use the dollar sign. At sign is for two and more, e.g. @total[1, 2].

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Hey Chroba, thanks for responding. Yes I use strict and warnings. I meant to delete those lines of code because they didn't work. I had them commented out.