blackjudas has asked for the wisdom of the Perl Monks concerning the following question:

Ok now that I feel like I have gotten over the grep hurdle, I've run into map and tried to apply it to part of the code I'm currently working on. Now, I've looked through any of the postings regarding map, though some of them are beyond my needs, so I've decided to experiment for a bit and ran into this snag:
my @sub_locs2; for (@session_keys) { if ($_ =~ /taxman.add.subloc./) { $_ =~ s/taxman.add.subloc.//g; push @sub_locs2, $_; } } my @sub_locs = map { $_ =~ s/taxman.add.subloc.//g if $_ =~ /taxman. +add.subloc./ } @session_keys;


@session_keys contains the following:
taxman.add.subloc.20 taxman.add.level taxman.add.value taxman.add.loc taxman.add.sel taxman.add.subloc.1 taxman.add.code taxman.add.subloc.16 taxman.add.subloc.19 taxman.add.name taxman.add.lvid

The for loop above does exactly what I want it to do, step through each element, modify it if it matches a pattern and push it into a new array. That's what map is supposed to do right? What it's doing now, is that map is affecting each element that matches the pattern and modifying the respective element in @session_keys, is this another precedence issue? Ok... what am I missing?

BJ

Replies are listed 'Best First'.
(tye)Re: Using map
by tye (Sage) on Oct 05, 2001 at 00:44 UTC

    Your for code has the exact same problem that you noted with your map code. In both cases, $_ becomes an alias for the original element and so modifying $_ modifies the original elements.

    So you need to copy each element, either before the loop or in the loop.

    There is another difference between your two samples. The if inside of the for means that non-matching items don't get put into the new array. But the map puts the empty string into the new array for each non-matching item. This is because the value returned by the statement $x if $y will be $y when $y is false.

    You are using . in your regex when you appear to want to match a literal ".". You may want to anchor that regex to start-of-line. You don't appear to actually need the /g modifier.

    So several fixes could yield you:

    my @sub_locs= map { local( $_ )= $_; $_ =~ s/^taxman\.add\.subloc\.// ? $_ : (); } @session_keys; my @sub_locs= map { s/^taxman\.add\.subloc\.// ? $_ : (); } @{[ @session_keys ]}; my @sub_locs= grep { s/^taxman\.add\.subloc\.// } @{[ @session_keys ]}; my @sub_locs= map { /^taxman\.add\.subloc\.(.*)/ } @session_keys;

            - tye (but my friends call me "Tye")
      Quite the few examples there tye! As for having the current value of $_ modified in the original in the for loop, I was aware of this happening, though what I guess I was trying to say is that my above map example did _only_ that, and just returned blank elements.

      Thanks for the extra effort tye, it makes all the difference, my "light" came on :) (rarely happens as you can tell :)

      BlackJudas
Re: Using map
by merlyn (Sage) on Oct 05, 2001 at 00:35 UTC
    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

      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

Re: Using map
by suaveant (Parson) on Oct 05, 2001 at 00:41 UTC
    Ok, for one...
    $_ =~ s/taxman.add.subloc.//g if $_ =~ /taxman.add.subloc./
    is rather a waste of time...
    s/taxman\.add\.subloc\.//g
    is sufficient since s will only do the replace if it matches, making the check for a match just slow you down... also you will notice that I removed $_ =~... s/// and // default to use $_, so it is just extra typing (you can do it if you want). I also changed your . to \. this is because . has special meaning in a regexp, which is that . will match any single character...

    As to why you are editing your main array. Map goes through your array and aliases each item of the array to $_. That means that any changes to $_ change the array items (this happens in for(@session_keys) as well), what you want to do is find the value you need and return it from the code block without editing $_... like so

    my @sub_locs = map { /taxman\.add\.subloc\.(.*)/ ? $1 : () } @session_ +keys;
    That should do what you want... the .* matches everything after taxman.add.subloc. and the parens around it tell perl to store what it finds in $1. the ? : says if I matched sucessfully, return $1, if not return () which means nothing will be put into @sub_locs unless there is a match...

    sound good?

    Update my using return was wrong.. fixed...

                    - Ant
                    - Some of my best work - Fish Dinner

      My apologies for posting bad code, the result you see above was a feeble attempt to see where I was going wrong because I was not receiving the results I was expecting. I apologise for this, my original code read:
      my @sub_locs = map { s/taxman.add.subloc.// } @session_keys;

      While I read up on most of the posts, the perldocs and looked at the examples in the camel book, the concept must have missed me by a mile. Thanks for clarifying, I am aware of the dots though for some reason I didn't escape them, I guess I got lazy since I used to escape them in previous code I've written, though I haven't experienced any funky results by not escaping them yet.

      BlackJudas
        You would only receive funky results if you had a file that had a character where you are looking for a . that was not a . say if you had a file name taxman.add2subloc.foo you would still match even though it has a 2 instead of a .
        And let me tell you... you don't not want to try to track that error down :) Those are the impossible to find errors :)

        and, as has been said, $_ inside the map code block is an alias to the item in the array, so any changes made directly to it (like substitutions) will edit the values in the array.

                        - Ant
                        - Some of my best work - Fish Dinner