Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

map and modifying $_

by Anonymous Monk
on Jul 14, 2000 at 22:53 UTC ( #22611=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

From the perldoc page for map:

<cite> Note that, because C<$_> is a reference into the list value, it can be used to modify the elements of the array. While this is useful and supported, it can cause bizarre results if the LIST is not a named array. Using a regular C<foreach> loop for this purpose would be clearer in most cases. </cite>

so now, what are the cases where map { s/foo/bar/ } @goo is different from for (@goo) { s/foo/bar/ }? what "bizarre results" is it talking about?

Replies are listed 'Best First'.
(chromatic) Re: map and modifying $_
by chromatic (Archbishop) on Jul 14, 2000 at 22:56 UTC
    @goo is a named array. However, if you use a list of some sort: map { s/\d/\\d/ } (1, 2, 3, 4);
    you'll get happy error messages like Modification of a read-only value attempted at - line 1.
Re: map and modifying $_
by Abigail (Deacon) on Jul 14, 2000 at 23:52 UTC
    I don't understand your question. @goo is a named array, and for (@goo) is not a C-style loop.

    Note that perl5.6 broke all programs that did something like map {s/foo/bar/} "foobar";. What has been gained by this breakage is totally unclear to me.

    -- Abigail

Re: map and modifying $_
by Anonymous Monk on Jul 16, 2000 at 18:13 UTC
    use of map in void context deprecated...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://22611]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2018-03-19 03:22 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (232 votes). Check out past polls.