Perl循环程序
我正试图编写一个程序来完成以下任务: 使用循环结构并对产生以下输出的程序进行编码(使用一个参数让用户指定需要打印的行数): 我的程序无法正常运行。当我试着运行它时,什么也没发生Perl循环程序,perl,Perl,我正试图编写一个程序来完成以下任务: 使用循环结构并对产生以下输出的程序进行编码(使用一个参数让用户指定需要打印的行数): 我的程序无法正常运行。当我试着运行它时,什么也没发生 #!/usr/local/bin/perl $A = 3; $B = 1; $i = 1; $output = ""; $j = 1; while ($i <= $ARGV[0]) { while ($j <= $i) { if ($A == 0 && $B == 0) {
#!/usr/local/bin/perl
$A = 3;
$B = 1;
$i = 1;
$output = "";
$j = 1;
while ($i <= $ARGV[0]) {
while ($j <= $i) {
if ($A == 0 && $B == 0) {
$A = 3;
$B = 1;
}
if ($A > 0) {
$output.= "A";
$A--;
}
else {
$output.= "B";
$B--;
}
&j++;
}
print($output . "/n");
$i++;
}
#/usr/local/bin/perl
$A=3;
$B=1;
$i=1;
$output=“”;
$j=1;
当我运行它时,$i会出现以下错误:
无法修改第行的非左值子例程调用
您使用了错误的标志。更改:
&j++;
致:
此外,您可能希望使用\n
而不是/n
此程序会按照您的要求执行。它希望列表项的数量作为参数在命令行上传递,如果指定了non,则默认为8个
use strict;
use warnings;
my $max = shift // 8;
my $string;
my @strings;
push @strings, $string .= $_ & 3 ? 'A' : 'B' for 1 .. $max;
print "@strings\n";
输出
A AA AAA AAAB AAABA AAABAA AAABAAA AAABAAAB
如果将&j++
更改为$j++
,并将打印($output./n”)
更改为打印($output.\n”)
,则程序将正常运行。不过,它确实需要一些整理,如下所示
$A = 3;
$B = 1;
$i = 1;
$output = "";
$j = 1;
while ($i <= $ARGV[0]) {
while ($j <= $i) {
if ($A == 0 && $B == 0) {
$A = 3;
$B = 1;
}
if ($A > 0) {
$output .= "A";
$A--;
}
else {
$output .= "B";
$B--;
}
$j++;
}
print($output . "\n");
$i++;
}
$A=3;
$B=1;
$i=1;
$output=“”;
$j=1;
而我
A AA AAA AAAB AAABA AAABAA AAABAAA AAABAAAB
$A = 3;
$B = 1;
$i = 1;
$output = "";
$j = 1;
while ($i <= $ARGV[0]) {
while ($j <= $i) {
if ($A == 0 && $B == 0) {
$A = 3;
$B = 1;
}
if ($A > 0) {
$output .= "A";
$A--;
}
else {
$output .= "B";
$B--;
}
$j++;
}
print($output . "\n");
$i++;
}