为什么我的 Perl 循环最后关闭了 1?

发布于 2024-09-28 21:42:51 字数 431 浏览 9 评论 0原文

我有这个程序,但它没有按预期工作。帮我。

我想打印行标题。

如果输入是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不会写诗 2024-10-05 21:42:51

问题就在这里:

my $count = @_;

分配发生在标量上下文中,它将数组@_中的元素数量分配给$count,您的case 是 1,因为您将 1 参数传递给函数。

要解决此问题,您可以这样做:

my ($count) = @_; 

或者

my $count = $_[0];

这是另一个问题:

for(my $i=1.....
    ^^

通过使用 my 您已将 $i local 设为 for 并且它在外部不可用。因此,您在 for 之外的最终 print 不会打印任何内容。要修复此问题,请将 $i 声明移到循环之外:

sub printC {
        my ($count) = @_;
        my $i;
        ....

始终将其写入

use strict;

程序的顶部,这样您就可以捕获此类错误。

执行函数操作的 perl 方式是:

print join('|',1..$count);

The problem is here:

my $count = @_;

The assignment is happening in a scalar context which assigns the number of elements in array @_ to $count, which your case is 1, as you are passing 1 argument to the function.

To fix this you can do:

my ($count) = @_; 

or

my $count = $_[0];

here is another problem:

for(my $i=1.....
    ^^

By using my you've made $i local to the body of for and it'll not be available outside it. So your final print outside the for is not printing anything. To fix this move the declaration of $i outside the loop:

sub printC {
        my ($count) = @_;
        my $i;
        ....

Always make it a point to write

use strict;

at the top of your program which enables you to catch such bugs.

The perl way of doing what your function does is:

print join('|',1..$count);
烂柯人 2024-10-05 21:42:51

一个真正的 Perl 黑客可能会写这样的东西:

sub printC {
  my $count = shift;
  print join "|", (1 .. $count);
}

一旦你理解了它是如何工作的,你会发现你已经了解了更多关于 Perl 的知识。 :-)

A real Perl hacker might write something like this:

sub printC {
  my $count = shift;
  print join "|", (1 .. $count);
}

Once you understand how this works, you'll find you've learned some more about Perl. :-)

伴我心暖 2024-10-05 21:42:51

一旦您离开 for 循环,由于声明的位置,$i 就不存在。

你可以做得

sub printC {
    my ($count) = @_;
    my $i;
    for ($i = 1; $i < $count; $i++) {
        print "$i|";
    }
    print $i;
}

更简单:

sub printC {
    my ($count) = @_;
    print join("|", 1 .. $count) . "\n";
}

$i does not exist once you've left the for loop because of where it's declared.

You could do

sub printC {
    my ($count) = @_;
    my $i;
    for ($i = 1; $i < $count; $i++) {
        print "$i|";
    }
    print $i;
}

Even simpler:

sub printC {
    my ($count) = @_;
    print join("|", 1 .. $count) . "\n";
}
与君绝 2024-10-05 21:42:51

“变得更Perlish”的另一点是不使用C 风格的for 循环。在 Perl 中几乎从来不需要使用 C 风格的 for

等价的

for(my $i=1;$i<$count;$i++) { ... }

它们几乎

for my $i (1 .. $count) { ... }

,除了后者更容易阅读并且更能抵抗差一错误。 (您的代码仅打印 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-style for in Perl.

Instead of

for(my $i=1;$i<$count;$i++) { ... }

use

for my $i (1 .. $count) { ... }

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 of 1|2|3|4 is because the test in your C-style for was checking $i<$count when it should have been $i<=$count - this is a very common mistake with C-style for loops which for (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 use printC($count) instead of &printC($count).

But, yeah, in this particular case, join is probably a better approach than for anyhow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文