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?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.