使用数组元素

时间:2016-01-04 22:40:14

标签: arrays perl loops if-statement

我有一个从0到31重复的字符串,在$GRIBWORKLIST2[$i]的整个序列中反复调用$i,一直到8,000(@GRIBWORKLIST == 8000)。

每当$GRIBWORKLIST2[$i+1]的值为0时,意味着$GRIBWORKLIST2[$i]的值完成一个循环并再次返回0。对于输出,只要发生这种情况,我希望它显示

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

以下是我尝试这样做的事。

任何想法如何调整它以便它会给我想要的结果?

for ( my $i = 0; $i < @GRIBWORKLIST; $i++ ) {

    if ( $i == ( @GRIBWORKLIST - 1 ) ) {

        print "----> ---> --> -> "
            . $GRIBWORKLIST[$i] . " | "
            . $GRIBWORKLIST2[$i] . " || "
            . $GRIBWORKLIST2[ $i - 1 ] . "\n";
    }
    else {

        print "----> ---> --> -> "
            . $GRIBWORKLIST[$i] . " | "
            . $GRIBWORKLIST2[$i] . " || "
            . $GRIBWORKLIST2[ $i - 1 ]
            . " |||  "
            . $GRIBWORKLIST2[ $i + 1 ] . "\n";

        my %q = $GRIBWORKLIST2[ $i + 1 ];

        print STDOUT "$q\n";

        if ( $q == 0 ) {
            print STDOUT "$$$$$$$$$$$$$$$$$$$$$$\n";
        }
    }
}

这给了我输出:

Can't use string ("32289") as a SCALAR ref while "strict refs" in use at ...

1 个答案:

答案 0 :(得分:1)

$$有意义,请参阅perlvar。使用单引号来避免美元插值。

print STDOUT '$$$$$$$$$$$$$$$$$$$$$$', "\n";