Perl数组被未知来源修改

Geo*_* H. 0 perl multidimensional-array

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

我们遇到的是,当for循环到达最后一个索引时,会推送一个空元素@questions.这会使结果增加scalar @questions1,并且该循环无限重复.我们无法弄清楚的是导致空元素被推送到@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++;
}
Run Code Online (Sandbox Code Playgroud)

更新

这是@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"
          ],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          [],
          []
        ];
Run Code Online (Sandbox Code Playgroud)

Gri*_*nnz 5

问题出在这里:

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

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

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

foreach my $i (0..$#questions) {
  # everything else the same
}
Run Code Online (Sandbox Code Playgroud)