Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

RE: Map: The Basics

by ahunter (Monk)
on Jul 05, 2000 at 11:32 UTC ( [id://21106]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Map: The Basics

splinky has got me doing it now... To pick a nit:

Map works on all types of lists, not just those returned by the <> operators. In addition, $_ is a symbolic reference (see perlref), which gives map yet another use, and makes one of your examples a bit confusing. Consider:

my @array = <FILE>; my @newarray = map { s/\#.*$//; $_ } @array;
Now @newarray and @array contain the same thing, as the s/// operator alters the contents of @array! Far better to write:
my @array = <FILE>; map s/\#.*$//, @array;
This way of doing things is deliberate, as it eliminates unnecessary assignments when you want the results to go back to the same array. The <FILE> thing is special, as perl copies the file to a temporary, anonymous, array for use while processing, so you never see that it gets altered.

Andrew.

Replies are listed 'Best First'.
Re: RE: Map: The Basics
by swiftone (Curate) on Jan 11, 2001 at 21:22 UTC
    my @array = <FILE>; map s/\#.*$//, @array;
    This way of doing things is deliberate, as it eliminates unnecessary assignments when you want the results to go back to the same array.

    Except that he explicitly said that if you aren't using the list returned, you shouldn't use map, you should look into another looping structure, such as:

    my @array = <FILE>; s/\#.*$// foreach @array;
    To do otherwise obfuscates the purpose of your loop.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://21106]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.