sorting - How do I sort a Perl hash that has tons of data? -


I'm sorting a hash in Perl I had to face a memory error while running my Perl script:

  foreach $ key (sort (keys (% hash))) {....}  

How can I sort a hash containing many data?

the ordered key hash a large % hash Is disabled, according to memory, its approximate equivalent:

  my @keys = keys% hash; @keys = sort @keys;  

There are three copies of keys in the memory while the sorting is done (one hash, one of the list of keys, one is made from the sorted list) Memory optimizations are not applicable for foreach .

Since hash is such a big, the best option is to get it out of memory completely, stick it in the Berkeley DB file. And if you want to keep the keys, then a hash is not the best choice, there is a tree. I had suggested using a Berkeley BTree file tree would keep your data sorted like an ash efficiently, while providing quick look like a hash.

Here's an example using. Simple and Better Document, but does not take advantage of the modern features of BerkleDeeBYMMV

Usage Berkeleybi; My $ db = My% hash tie, 'Berkeleyby :: Betty', -Filename = & gt; "Your.db", -Compare = & gt; Sub {$ _ [1] CMP $ _ [0]}, -flags => DB_CREATE;

-Compare shows how to provide your own sorting function will stop the tied interface. Unless you need to act like a hash, use the object interface.


Comments