perl,一个简单的哈希
程序如下:my@people=qw{fredbarneyfredwlimadinobarneyfredpebbles};my%count;$count{$_}++fore...
程序如下:
my @people = qw { fred barney fred wlima dino barney fred pebbles };
my %count ;
$count{$_}++ foreach @people;
我想打印最终$count{ "fred" }的哈希值,请问接下来该怎么写? fred的哈希值是不是应该是3啊? 展开
my @people = qw { fred barney fred wlima dino barney fred pebbles };
my %count ;
$count{$_}++ foreach @people;
我想打印最终$count{ "fred" }的哈希值,请问接下来该怎么写? fred的哈希值是不是应该是3啊? 展开
2个回答
展开全部
如果我这么写:print "$count{ "fred" }"; (在哈希前后加上双引号)程序就会报错:
Unquoted string "fred" may clash with future reserved word at ./perl.pl line 762.
String found where operator expected at ./perl.pl line 762, near "fred" }""
syntax error at ./perl.pl line 762, at EOF
Execution of ./perl.pl aborted due to compilation errors.
这是为什么啊?
那是因为你的“”里面还有“”,而perl会把第二个“和前面的”匹配,导致出错,你可以这样
print "$count{ \"fred\" }";
Unquoted string "fred" may clash with future reserved word at ./perl.pl line 762.
String found where operator expected at ./perl.pl line 762, near "fred" }""
syntax error at ./perl.pl line 762, at EOF
Execution of ./perl.pl aborted due to compilation errors.
这是为什么啊?
那是因为你的“”里面还有“”,而perl会把第二个“和前面的”匹配,导致出错,你可以这样
print "$count{ \"fred\" }";
展开全部
直接print $count{ "fred" }就可以
追问
谢谢。
如果我这么写:print "$count{ "fred" }"; (在哈希前后加上双引号)程序就会报错:
Unquoted string "fred" may clash with future reserved word at ./perl.pl line 762.
String found where operator expected at ./perl.pl line 762, near "fred" }""
syntax error at ./perl.pl line 762, at EOF
Execution of ./perl.pl aborted due to compilation errors.
这是为什么啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询