in reply to How to filter few key value pairs from hash reference

I assume the data in your array of hashes will be sourced from an external file and will be much larger than a few records. Why then not transform that external file into a "real" database which you can manipulate with standard SQL queries? In the long run that will save time and effort.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics
  • Comment on Re: How to filter few key value pairs from hash reference

Replies are listed 'Best First'.
Re^2: How to filter few key value pairs from hash reference
by jaypal (Beadle) on Sep 15, 2014 at 22:10 UTC

    Thanks CountZero for the suggestion. This was just a learning exercise where I was looking for copying a subset of a hash. Typically, I would use a hash slice but here I was looking to copy the key value pairs.

    The map solution posted by choroba seems to be exactly what I needed (without creating a temporary copy of hash reference) though impairs readability (probably due to ternary op). Solution by JohnGG looks promising and quite readable but may not scale well if we have large sets of key value pairs to keep.

    Still hoping to find a sweet spot between the two solutions.

      Typically, I would use a hash slice
      Time to upgrade to 5.20, it seems: New slice syntax.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ