perl - Dynamically creating an associative array of arrays -
i'm trying dynamically create associative array values arrays. current attempt follows i'm not sure if correct or efficent.
foreach $line (@lines) # read line text dictionary { chomp( $line ); my($word, $definition) = split(/\s/, $line, 2); # $definition =~ s/^\s+|\s+$//g ; # trim leading , trailing whitespace if( exists $dict{$word} ) { @array = $dict{$word}; $len = scalar @array; $dict{$word}[$len] = $definition; } else { $dict{$word}[0] = $definition; } }
pretty sure works (can't test right now)
foreach $line (@lines) # read line text dictionary { chomp( $line ); my($word, $definition) = split(/\s/, $line, 2); # $definition =~ s/^\s+|\s+$//g ; # trim leading , trailing whitespace push @{$dict{$word}}, $definition; }
(using unshift instead of push put new entry on other side of other entries)
Comments
Post a Comment