in reply to Using map

Actually, they kinda do the same thing, except that the map block returns the last expression evaluated, which is what is being gathered to make the output value.

If I follow your quest, you are looking to take @session_keys and grab any entry that contains the taxman string, but only the part after the taxman string. I think this is closer:

my @sub_locs = map { /taxman.add.subloc.(.*)/ ? $1 : () } @session_keys;
If the regex matches, then $1 is added to the output list. If the regex doesn't match, then an empty list is added, which means the item is essentially ignored.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Using map
by Masem (Monsignor) on Oct 05, 2001 at 00:42 UTC
    Possibly a bit more 'intuitive'...
    my @sub_locs = grep { $_ } map { /taxman\.add\.subloc\.(.*)/ } @sessio +n_keys;
    ...though YMMV depending on comfort with the language.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      my @sub_locs = grep { $_ } map { /taxman\.add\.subloc\.(.*)/ } @sessio +n_keys;
      Maybe a "bit more intuitive", but "wrong" if the value after taxman string might be empty or the value "0". Did you mean instead:
      my @sub_locs = grep { defined $_ } map { /taxman\.add\.subloc\.(.*)/ } + @session_keys;
      perhaps?

      And of course, as I'm seeing this, the grep isn't even necessary, since on a failed match, the match returns an empty list!

      my @sub_locs = map { /taxman\.add\.subloc\.(.+)/ } @session_keys;
      is enough!

      -- Randal L. Schwartz, Perl hacker