http://www.perlmonks.org?node_id=1038348


in reply to why use a hash instead of an array

If I had to choose between a language having arrays but no hashes, and a languages having hashes and no arrays, I would no doubt select the one with hashes (I have used at least a dozen other languages for serious purposes, and only one, besides Perl, namely Python, had hashes, at least natively, so I think I can claim that I know what it is to have hashes or not to have them). Just about everything that can be done with arrays can be done with hashes (if only by using the array subscripts as keys to the hash), but the reverse is really not true. Once you acquire a bit more experience with Perl or any other language having hashes (which sometimes have different names, they can be dictionnaries or associative arrays, or whatever), you will understand how immensely useful they are.

Just one example from my work, something which I am due to present in a Perl conference in Europe at the end of this week. I had a program written in a database exploration language that I will call G. G is a fine and rather fast language, but it does not have hashes (and, to me, it is by far its single most important defect). The program was running on a very large database and making very complex comparisons with a relatively large parameter file. The first time we ran it, we stopped it after a couple of days when we found that it was going to take about 180 days to complete. After some optimization work, I ended up with a predicted execution time of 60 days, and with no idea for further optimization. Of course, a 60-day running time was not acceptable (in the business context, 3 to 5 days would have been not ideal but still OK, but certainly not 60 days). I decided to completely rewrite the program in Perl using four simple hashes, two hashes of hashes and one hash of hashes of hashes. The program now runs in about one hour (about 1,400 shorter execution time). To make things clear, it is not by simply replacing the previous data structure by some hashes that I obtained this huge improvement, it is because hashes enabled me to use a completely different and far better algorithm.

Just one last thing, don't understand the above wrongly. My point is NOT that hashes are fast (they are fast, and this is important, but that is not my point). My point is that they very often allow for much better data structures and much better algorithms for many many tasks.