Victor Peinado wrote: > Hi list, > > I have a bilingual dictionary in a file with the following format: > > word1:sense1 sense2 ... senseN > word2:sense1 sense2 ... senseN > ... > > I'm doing a simple script which looks for a word and returns all the > possible translations. What's the faster (or the more efficient) > way to do that when the dictionary gets bigger? A while loop > reading line by line and evaluating which is the current word, or > loading the dictionary in a hash of arrays before the search? >
If you can load your entire database into a hash and leave it there for multiple accesses then that's quickest. If you're reading the file each time you do a lookup then just read the file line by line until you find what you want. On average you'll have to read half of the file before you find what you want, whereas if you load it into a temporary hash you, obviously, have to read all of it. If your data file is sorted you can write something fancy to do a binary search. The nicest way is to build a permanent hash of the words against the position of their entries in the file. That way your hash should be reasonably small and you can fetch the data in a single read. If the file is static (doesn't change much) then you could build an index file by creating your hash of file addresses and then saving it to disk with a module like Storable. Then it's just a matter of loading the index hash from disk, looking up the position in the data file and reading the data. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
