我正在尝试动态创建一个关联数组,其值为数组。我目前的尝试如下,但我不确定它是否正确或有效。
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;
}
}