|Perl: the Markov chain saw|
A more elegant way to filter a nested hash?by jimpudar (Pilgrim)
|on May 31, 2018 at 00:19 UTC||Need Help??|
jimpudar has asked for the wisdom of the Perl Monks concerning the following question:
Hello brothers and sisters of the monastary,
I need a function which will filter a nested hash, removing any fields I'm not interested in.
For example, lets say my application uses the following data structure. The hash values in any given instance of this structure are variable, but the hash keys will always be the same.
I only am interested in the important data, and I want to remove all the garbage.
I came up with a data structure I could use to express the parts of the structure I'm interested in.
Given this filter, I want the output of the function to be the following hash:
A key feature of this function is that it must accept any variable type of filter; data which is garbage in one case is important in another case.
I came up with the following function which seems to work well:
I'm very happy with how it works, but since I mean for this function to be reusable I wanted to hear how you all would do it.
Is there a simpler way to do this with higher order functions like map and grep?
Thank you for your help,
πάντων χρημάτων μέτρον έστιν άνθρωπος.