为什么我的 Perl 循环最后关闭了 1?
我有这个程序,但它没有按预期工作。帮我。
我想打印行标题。
如果输入是 4
,我希望输出 1|2|3|4
。
它不能正常工作,如果我硬编码 $count
值,它可以部分工作,但最后一个数字丢失。
sub printC {
my $count = @_;
# count = 4 # works partially only prints 1|2|3
for(my $i=1;$i<$count;$i++) {
print "$i|";
}
print $i;
}
$count = 2;
&printC($count);
print "\n";
I have this program which does not work as expected. Help me.
I want to print a row heading.
If input is 4
, I want 1|2|3|4
to be output.
It does not work as all, if I hard-code $count
value it works partially but the last number is missing.
sub printC {
my $count = @_;
# count = 4 # works partially only prints 1|2|3
for(my $i=1;$i<$count;$i++) {
print "$i|";
}
print $i;
}
$count = 2;
&printC($count);
print "\n";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题就在这里:
分配发生在标量上下文中,它将数组
@_
中的元素数量分配给$count
,您的case 是1
,因为您将1
参数传递给函数。要解决此问题,您可以这样做:
或者
这是另一个问题:
通过使用
my
您已将$i
local 设为for
并且它在外部不可用。因此,您在for
之外的最终print
不会打印任何内容。要修复此问题,请将$i
声明移到循环之外:始终将其写入
程序的顶部,这样您就可以捕获此类错误。
执行函数操作的 perl 方式是:
The problem is here:
The assignment is happening in a scalar context which assigns the number of elements in array
@_
to$count
, which your case is1
, as you are passing1
argument to the function.To fix this you can do:
or
here is another problem:
By using
my
you've made$i
local to the body offor
and it'll not be available outside it. So your finalprint
outside thefor
is not printing anything. To fix this move the declaration of$i
outside the loop:Always make it a point to write
at the top of your program which enables you to catch such bugs.
The perl way of doing what your function does is:
一个真正的 Perl 黑客可能会写这样的东西:
一旦你理解了它是如何工作的,你会发现你已经了解了更多关于 Perl 的知识。 :-)
A real Perl hacker might write something like this:
Once you understand how this works, you'll find you've learned some more about Perl. :-)
一旦您离开
for
循环,由于声明的位置,$i
就不存在。你可以做得
更简单:
$i
does not exist once you've left thefor
loop because of where it's declared.You could do
Even simpler:
“变得更Perlish”的另一点是不使用C 风格的
for
循环。在 Perl 中几乎从来不需要使用 C 风格的for
。等价的
它们几乎是
,除了后者更容易阅读并且更能抵抗差一错误。 (您的代码仅打印
1|2|3
而不是1|2|3|4
的原因是因为您的 C 风格for< 中的测试/code> 正在检查
$i<$count
,而它应该是$i<=$count
- 这是 C 风格中一个非常常见的错误
循环,for (list)
完全避免了。)此外,不要在子调用前加上
&
前缀。它是 Perl 4 的遗留物,在 Perl 5 中不再需要,并且具有您可能不知道也可能不想要的副作用。只需使用printC($count)
而不是&printC($count)
。但是,是的,在这种特殊情况下,
join
可能是比for
更好的方法。One other bit of "being more Perlish" is to not use the C-style
for
loop. There is almost never a need to use a C-stylefor
in Perl.Instead of
use
They're almost equivalent, except the latter version is both more easily readable and more resistant to off-by-one errors. (The reason your code was only printing
1|2|3
instead of1|2|3|4
is because the test in your C-stylefor
was checking$i<$count
when it should have been$i<=$count
- this is a very common mistake with C-stylefor
loops whichfor (list)
avoids completely.)Also, don't prefix your sub calls with
&
. It's a holdover from Perl 4 which is no longer needed in Perl 5 and has side effects which you probably aren't aware of and probably don't want. Just useprintC($count)
instead of&printC($count)
.But, yeah, in this particular case,
join
is probably a better approach thanfor
anyhow.