in reply to Re^2: Get out of "For Loop"!
in thread Get out of "For Loop"!

chromatic,
Why map and not grep?

Perhaps I am missing something, but grep doesn't look reasonable here. This is both filtering and transforming which makes me think map. In addition to filtering hash references based on the 'email' key, it returning the value of the 'name' key. I can't think of a way to do that with grep that is clear but please feel free to elucidate me?

Cheers - L~R

Replies are listed 'Best First'.
Re^4: Get out of "For Loop"!
by ikegami (Patriarch) on Oct 12, 2010 at 22:59 UTC

    There is no translation in the OP, and I suspect it's undesirable to drop the email addresses.

      ikegami,
      The node to which chromatic replied, says: I'm assuming you want to do something useful with the list of cases where there is "No email address". If so, 'map' is your friend:

      I interpreted what NetWallah wrote as assuming the OP might want to do something more than what was posted and offered a suggestion. I know chromatic is a smart guy, so if he had meant - you shouldn't be transforming here, you should just be filtering (map vs grep) then I understand the comment. But the code block as written, which I am sure NetWallah understood is most naturally written as map not grep (at least to me) is what chromatic appeared to be responding to (at least to me). That is why I asked for elucidation. Your response, which is a legitimate interpretation of comment - is not what I took away. Especially given it was so terse.

      Cheers - L~R

        I interpreted what NetWallah wrote as assuming the OP might want to do something more than what was posted

        Me too. Why skip entries with no emails if you're not going to use the emails?

        I could see

        my @emails = map { $_->{email} eq '' ? () : $_->{email} } @$data;
        although it might be clearer to do
        my @emails = grep $_, map $_->{email}, @$data;
        I just saw these updates. Thanks for coming to my defense!

        The strange thing is - when I read chromatic's question, and re-read my code a day later, I did have to re-think for a minute about why I used map instead of grep. You were spot-on.

             Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re^4: Get out of "For Loop"!
by chromatic (Archbishop) on Oct 13, 2010 at 00:49 UTC

    I misread NetWallah's code in light of the OP's code.