Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found