首页 > 解决方案 > Perl 数组被未知来源修改

问题描述

我相信我遇到了一个错误,但想确定我不只是错过了什么。我的实习生正在网站上工作,该网站将对数量不定的问题进行测试,并提供可变数量的答案供员工完成培训。我们将问题存储在一个二维数组中,其中包含问题 ID 和问题文本。我们循环遍历该数组为每个问题创建 div,隐藏除第一个之外的所有问题,并使用该问题 ID 在循环中获取该问题的答案。

但是我们遇到的是,当for循环到达最后一个索引时,一个空元素被推送到@questions. 这将结果增加scalar @questions一,并且此循环无限重复。我们无法弄清楚是什么导致了一个空元素被推送到@questions?

my @questions;

my $getQuestion = $dbh->prepare("
    SELECT ID, Question 
    FROM ACTestQuestions
    WHERE TestID = ?
");

$getQuestion->execute(1);

while(my @question = $getQuestion->fetchrow_array()){
    push(@questions, \@question);
}

my $sth = $dbh->prepare("
    SELECT ID, AnswerText, CorrectAnswer
    FROM ACTestAnswers
    WHERE QuestionID = ?
    ORDER BY SortOrder
");

# Irrelevant parts skipped

for(my $i = 0; $i < scalar @questions; $i++){
    my $qCount = 1;
    my $qID = $questions[$i][0];
    my $hideClass = "hide";

    if($i == 0){
        $hideClass = "";
    }
    print <<"END_HTML";
    <div id="question-$questions[$i][0]"  class="centered question-container $hideClass">
        <h1 class="text-center">Question &num;$qCount</h1>
        <h4 class="text-center" >$questions[$i][1]</h4></br>
            <form method="get">
                <table>                     
END_HTML

    if($sth->execute($questions[$i][0])){
        $num = 1;
        while(($ansID, $ansText, $correct) = $sth->fetchrow_array()){
            print <<"END_HTML";
            <tr>
            <td style="padding: 15px 30px"> 
                <label>
                    <input type="radio" name="questionAnswer" id="$ansID" value="$ansID" />
                    $num. $ansText
                </label>
            </td>
            </tr>
END_HTML
            $num++;
        }
    }

    print <<"END_HTML";
                </table>
                <div class="text-center" style="padding: 25px; font-weight: bold;">
                    $ans
                </div>
                <div class="tButton">
                    <input type="submit" name="submit" value="Submit"/>
                    <button type="button" id="prev" class="prev" onclick="prevQuestion($questions[$i-1][0])">&laquo; prev
                    <button type="button" id="next" class="next" onclick="nextQuestion($questions[$i+1][0])">next &raquo;
                </div>
            </form>
    </div>
END_HTML
    $qCount++;
}

更新

@questions这是循环前后的转储。我使用硬编码$i < 18来获得这个结果,但它会一直持续到内存不足。

[10/24/18 17:25:05] $VAR1 = [
          [
            9,
            'What are the key ingredients in the Original Killer Brownie&reg;?'
          ],
          [
            10,
            'Where do our Laura&#39;s Cookies come from?'
          ],
          [
            11,
            'How long is the shelf life on our artisan breads?'
          ],
          [
            12,
            'What happens to the bakery shrink (the items no longer fresh enough to sell)?'
          ],
          [
            13,
            'How many days a week are DLM artisan breads produced?'
          ],
          [
            14,
            'DLM artisan breads are produced in a ____ style oven.'
          ],
          [
            15,
            'How many items does Central Bakery produce for the stores?'
          ],
          [
            16,
            "TEST"
          ]
        ];
[10/24/18 17:25:05] $VAR1 = [
          [
            9,
            'What are the key ingredients in the Original Killer Brownie&reg;?'
          ],
          [
            10,
            'Where do our Laura&#39;s Cookies come from?'
          ],
          [
            11,
            'How long is the shelf life on our artisan breads?'
          ],
          [
            12,
            'What happens to the bakery shrink (the items no longer fresh enough to sell)?'
          ],
          [
            13,
            'How many days a week are DLM artisan breads produced?'
          ],
          [
            14,
            'DLM artisan breads are produced in a ____ style oven.'
          ],
          [
            15,
            'How many items does Central Bakery produce for the stores?'
          ],
          [
            16,
            "TEST"
          ],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          []
        ];

标签: perlmultidimensional-array

解决方案


问题在这里:

<button type="button" id="next" class="next" onclick="nextQuestion($questions[$i+1][0])">next &raquo;

该表达式$questions[$i+1][0]会将元素自动激活$questions[$i+1]为数组引用,因为您像访问它一样访问它。因此@questions将有一个额外的元素。您可以使用autovivification pragma 来防止这种情况发生,或者在使用它之前检查它$questions[$i+1]是否已定义或是否已定义。是数组最后一个索引的快捷方式。注意:您可能对 prevQuestion 行和索引 0 有类似的问题,尽管索引 -1 只会访问数组的最后一个元素。$i < $#questions$#questions@questions

这个问题的另一个角度是,您的 C 风格 for 循环取决于@questions每次迭代的大小。另一种方法是使用 Perlish foreach 循环

foreach my $i (0..$#questions) {
  # everything else the same
}

推荐阅读