我正在尝试动态创建一个值为数组的关联数组。我的当前尝试如下,但我不确定它是否正确或有效。
foreach $line (@lines) # read line from a text dictionary
{
chomp( $line );
my($word, $definition) = split(/\s/, $line, 2); #
$definition =~ s/^\s+|\s+$//g ; # trim leading and trailing whitespace
if( exists $dict{$word} )
{
@array = $dict{$word};
$len = scalar @array;
$dict{$word}[$len] = $definition;
}
else
{
$dict{$word}[0] = $definition;
}
}
push @$dict{$word}, $definition
来替换整个if
/else
语句。目前你只是为了计算数组长度而复制了一遍数组。 - NemoNot an ARRAY reference
。 - Olumide