为什么哈希在打印输出时删除第一个值apple:2?
use warnings; use strict; use Data::Dumper; my @array = ("apple:2","pie:4","cake:2"); my %wordcount; our $curword; our $curnum; foreach (@array) { ($curword,$curnum) = split(":",$_); $wordcount{$curnum}=$curword; } print Dumper (\%wordcount);
解决方法
Perl哈希只能有唯一的密钥,所以
$wordcount{2} = "apple";
后来被覆盖了
$wordcount{2} = "cake";