perl6/rakudo:在循环变量上写入时出现问题
#!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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您的第二个示例
<->
可能无法在您使用的 Rakudo Perl 中工作,但它已在更新的版本中修复。 (这与深度解析问题有关,需要比当时更好的最长令牌匹配算法。)关于您的第三个示例
该语句
将
$list
创建为Seq
数据类型和Seq
元素被认为是不可变的。您真正想要的是$list
成为一个Array
,如下所示:有了它,最后一个示例将按预期工作:
希望这会有所帮助!
下午
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
creates
$list
as aSeq
data type, andSeq
elements are considered to be immutable. What you really want is for$list
to become anArray
, as in:With that in place, the last example works as expected:
Hope this helps!
Pm