in reply to Re^2: map and grep (but for hashes)
in thread map and grep (but for hashes)

I agree, I do find that I almost always need just the keys. I think I just always end up thinking too much about so I come up with something convoluted. It's like my brain turns off when it comes to map and hashes.

BTW, that should be @new_keys = grep, not map

Replies are listed 'Best First'.
Re^4: map and grep (but for hashes)
by zerohero (Monk) on Jan 30, 2009 at 20:13 UTC

    OK, wait, you are not Captain Kirk, this is not the Kobayashi Maru, and you do not get to change the conditions of the test to save the ship and defeat the Klingons. Always reducing things to arrays...it's like blending things that shouldn't be blended.

    It's actually extremely beneficial to be able to have operations that give you hashes back.

    It seems like there's no terse ways to do this in an expression, and it would be better to do it in a little routine.

Re^4: map and grep (but for hashes)
by zerohero (Monk) on Jan 30, 2009 at 20:32 UTC

    That's mostly how I've been operating, but I don't like always breaking things down into lists. It's good for algorithms, but you tend to lose structure.

    The hmap and hgrep methods seem like a nice way to do that, because when I'm putting things together, it seems like they want to go into a big anonymous structure (multi-level hash). Think of assembling JSON like things for example. In these cases I find it more natural to work with hashes, and hashes of hashes and so forth.

    By the answers it seems like the shortest thing is a few lines, which is not overly long, but lacks the super terseness of map or grep. So I think that means I need to write a little function. This isn't so hard, but it does seem that map and grep are so versatile, I was wondering if there were aspects of them I was "not getting", and therefore if there were some ultra-compact expression. It seems like 3 lines is about as short as you can go.

    Thanks, Roger