Interesting problem. Here are some tangential thoughts,

Your hash_filter() at the leaf nodes of the source hash copies the corresponding scalar values. Instead it could just get a reference to them so that the result of the filter will be live-linked with source hash. If such a feature is desirable. Maybe something like: $output{$_} = \$source->{$_}; A scenario would be that you create an empty hash with certain fixed structure, live-linked filter it and then whenever you populate it (assuming data of the same structure), results "miraculously" will pop out at the other end of the live-linked filtered results! Albeit they will be overwritten by the next time of use.

Then, for each different data structure you may have its own live-linked filter thus eliminating parsing the source hash every time.

Secondly, in your code you may want to keep a cache storing filter results and keyed on source+filter *keys* and overall nested hash structure. This calls for a function which calculates a hash-code(=unique signature) of a hash-or-array based only on keys and nested-structure. Not values. It will be similar to your code and recursive. However, I can see no time savings to calculate a hash-code (every time) and retrieve from a cache the filtered result compared to just filter every time. But it would be cool to have such a function (I can write one if anyone is interested) and even cooler if it was incorporated into a special Hash/Array module which would uodate the hash-code at every insert/delete of keys (definetely not my cup of tea).

Edit: Sumup: So the last suggestion is a way to see if two nested data structures comprising of hashes and arrays have THE SAME STRUCTURE. The challenge is to somehow make this as efficient as possible and definetely not to have to calculate it every time it is required given that the data structure has not changed.


In reply to Re: A more elegant way to filter a nested hash? by bliako
in thread A more elegant way to filter a nested hash? by jimpudar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.