sid*_*com 3 for-loop read-write perl6 rakudo-star raku
#!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;
}
Run Code Online (Sandbox Code Playgroud)
这有效.但在第二和第三,我得到一个错误信息.我怎么能让他们工作?
#!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 ""
Run Code Online (Sandbox Code Playgroud)
.
#!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
Run Code Online (Sandbox Code Playgroud)
小智 7
将<->在您使用Perl的Rakudo可能没有工作,但它已被固定在最近的版本.(这与深度解析问题有关,需要一个比我们当时更好的最长令牌匹配算法.)
该声明
my $list = <a b c d e f>;
Run Code Online (Sandbox Code Playgroud)
创建$list为Seq数据类型,Seq元素被认为是不可变的.你真正想要的是$list成为一个Array,如:
my $list = [<a b c d e f>];
Run Code Online (Sandbox Code Playgroud)
有了这个,最后一个例子按预期工作:
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$
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
下午