Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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?

Comment on map and modifying $_
Select or Download Code
(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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://22611]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-08-20 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (125 votes), past polls