为什么Perl函数“map”给出错误“没有足够的地图参数”
发布时间:2020-12-15 21:39:12 所属栏目:大数据 来源:网络整理
导读:这是我不明白的事情. 此脚本正常工作(注意地图功能中的连接): #!/usr/bin/perluse strict;use warnings;use Data::Dumper;my %aa = map { 'a' . '' = 1 } (1..3);print Dumper %aa;__END__output:$VAR1 = { 'a' = 1 }; 但是没有连接,地图就不起作用了.这是
这是我不明白的事情.
此脚本正常工作(注意地图功能中的连接): #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %aa = map { 'a' . '' => 1 } (1..3); print Dumper %aa; __END__ output: $VAR1 = { 'a' => 1 }; 但是没有连接,地图就不起作用了.这是我期望工作的脚本,但它没有: #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %aa = map { 'a' => 1 } (1..3); print Dumper %aa; __END__ output: Not enough arguments for map at e.pl line 7,near "} (" syntax error at e.pl line 7,near "} (" Global symbol "%aa" requires explicit package name at e.pl line 9. Execution of e.pl aborted due to compilation errors. 你能解释一下这种行为吗? 解决方法
Perl使用启发式方法来决定您是否正在使用:
map { STATEMENTS } LIST; # or map EXPR,LIST; 因为虽然“{”通常是块的开头,但它也可能是hashref的开头. 这些启发式方法在令牌流(IIRC两个令牌)中看不到很远. 您可以使用以下方法强制“{”被解释为块: map {; STATEMENTS } LIST; # the semicolon acts as a disambigator 您可以使用以下方法强制“{”解释为哈希: map +{ LIST },LIST; # the plus sign acts as a disambigator grep同样受苦. (技术上也是如此,因为hashref可以作为参数给出,然后将其字符串化并将其视为文件名.虽然这很奇怪.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |