perl6/rakudo:在循环变量上写入时出现问题

发布于 2024-10-18 05:03:27 字数 881 浏览 9 评论 0原文

#!perl6
use v6;

my $longest = 3;
my @list = <a b c d e f>;

for @list -> $element is rw {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

这有效。但在第二次和第三次我收到一条错误消息。我怎样才能让它们发挥作用?

#!perl6
use v6;

my $longest = 3;
my @list = <a b c d e f>;

for @list <-> $element {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

# ===SORRY!===
# Missing block at line 11, near ""

#!perl6
use v6;

my $longest = 3;
my $list = <a b c d e f>;

for $list.list -> $element is rw {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

# Cannot modify readonly value
#   in '&infix:<=>' at line 1
#   in <anon> at line 8:./perl5.pl
#   in main program body at line 1
#!perl6
use v6;

my $longest = 3;
my @list = <a b c d e f>;

for @list -> $element is rw {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

This works. But in the second and third I get an error-message. How could I make them work?

#!perl6
use v6;

my $longest = 3;
my @list = <a b c d e f>;

for @list <-> $element {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

# ===SORRY!===
# Missing block at line 11, near ""

.

#!perl6
use v6;

my $longest = 3;
my $list = <a b c d e f>;

for $list.list -> $element is rw {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

# Cannot modify readonly value
#   in '&infix:<=>' at line 1
#   in <anon> at line 8:./perl5.pl
#   in main program body at line 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

清风疏影 2024-10-25 05:03:27

关于您的第二个示例

<-> 可能无法在您使用的 Rakudo Perl 中工作,但它已在更新的版本中修复。 (这与深度解析问题有关,需要比当时更好的最长令牌匹配算法。)

关于您的第三个示例

该语句

my $list = <a b c d e f>;

$list 创建为 Seq 数据类型和 Seq 元素被认为是不可变的。您真正想要的是 $list 成为一个 Array,如下所示:

my $list = [<a b c d e f>];

有了它,最后一个示例将按预期工作:

pmichaud@orange:~/rakudo$ cat x.p6
#!perl6
use v6;

my $longest = 3;
my $list = [<a b c d e f>];

for $list.list -> $element is rw {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

pmichaud@orange:~/rakudo$ ./perl6 x.p6
  a
  b
  c
  d
  e
  f
pmichaud@orange:~/rakudo$ 

希望这会有所帮助!

下午

Regarding your second example

The <-> may not have worked in the Rakudo Perl you used, but it has been fixed in more recent versions. (It had to do with a deep parsing issue that required a better longest-token-matching algorithm than we had at that time.)

Regarding your third example

The statement

my $list = <a b c d e f>;

creates $list as a Seq data type, and Seq elements are considered to be immutable. What you really want is for $list to become an Array, as in:

my $list = [<a b c d e f>];

With that in place, the last example works as expected:

pmichaud@orange:~/rakudo$ cat x.p6
#!perl6
use v6;

my $longest = 3;
my $list = [<a b c d e f>];

for $list.list -> $element is rw {
    $element = sprintf "%*.*s", $longest, $longest, $element;
    $element.say;
}

pmichaud@orange:~/rakudo$ ./perl6 x.p6
  a
  b
  c
  d
  e
  f
pmichaud@orange:~/rakudo$ 

Hope this helps!

Pm

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