如何在Perl的fetchall_arrayref foreach循环中获取数组的最后一个元素? [英] How can I get the last element of an array inside of my fetchall_arrayref foreach loop in Perl?

查看:151
本文介绍了如何在Perl的fetchall_arrayref foreach循环中获取数组的最后一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Perl与MySql一起使用.我有一个查询,该查询与fetchall_arrayref一起使用,以从调查应用程序中获取结果数据.根据文档的建议,我使用foreach遍历fetchall_arrayref.在此循环内,我遍历$ answer1数据并运行一个计数器以对问题的所有答案进行计数,并希望得到总计.当我将此信息推送到数组中并尝试获取该数组中的最后一个元素时,我没有得到所需的输出.我使用相同的逻辑在fetchall_arrayref foreach循环之外进行了测试,并且成功地获得了我所需要的东西.

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.

这是我的代码:

my $testQuery = "SELECT questionNum, question, answer1 FROM results WHERE 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 $previous_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

当我打印时:"say @total [-1];"给我输出"1 2 3"当我只需要"3"时.

When I print: "say @total[-1];" gives me the output of "1 2 3" when I just need "3".

我在fetchall_arrayref foreach循环之外测试了相同的逻辑,并且得到了所需的输出:

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 @totalCounting, $counting;
}
say @totalCounting[-1];

"say totalCounting [-1];"给我"5".

"say totalCounting[-1];" gives me "5".

为什么在fetchall_arrayref内不起作用?

Why won't this work inside of fetchall_arrayref?

推荐答案

您的第一段代码等同于

my $counting;
my @totalCounting;
my @link = ('water', 'water', 'water', 'water', 'water');
foreach my $i (@link){
    $counting++;
    push @totalCounting, $counting;
    say @totalCounting[-1];
}

如果您想在循环之后执行某些操作,请将其放置在循环之后而不是将其多次执行的地方.

If you want to do something after the loop, place it after the loop and not in it where it will get executed multiple times.

使用数组仅使用其最后一个元素的整个概念都是有缺陷的.更简单,更浪费:

The whole concept of using an array to only use its last element is flawed. Simpler and last wasteful:

my @link = ('water', 'water', 'water', 'water', 'water');

my $counting;
for my $i (@link) {
    $counting++;
}

say $counting;

(当然,您可以在这里简单地使用 say 0 + @ link; ,但可以理解的是,该代码是有条件计数的较大代码段的代理.)

(Of course, you could simply use say 0+@link; here, but it's understood this code is a proxy for a larger snippet where the counting is conditional.)

是否可以在fetchall_arrayref中获取最后一个元素?

$ total [-1] (因此, @total [-1] ) @的最后一个元素总计.

好吧,这是当前存在的最后一个元素.您显然无法获得尚未放置在 @total 中的元素.但这正是您想做的.如果在循环完成之前需要总计,则需要先进行计算.

Well, it's the last element as it currently exists. You obviously can't get an element you haven't placed in @total yet. But that's exactly what it sounds like you want to to do. If you need the total before the loop is done, you will need to calculate it before you need it.

my $total;
for my $data (@$potential) {
    my ($questionNumber, $question, $answer1) = @$data;
    if ($answer1 ne "" && $questionNumber == 1) {
        $total++;
    }
}

for my $data (@$potential) {
    my ($questionNumber, $question, $answer1) = @$data;
    # Do something that uses $total...
}

这篇关于如何在Perl的fetchall_arrayref foreach循环中获取数组的最后一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆