Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

RE: Map: The Basics

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


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 12, 2001 at 02: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
Node Status?
node history
Node Type: note [id://21106]
help
Chatterbox?
[Corion]: choroba: Yeah, I think that would be the good solution
[LanX]: I suspect the first string which comes from the DB ...
[LanX]: ... but this part is already in production for a year now
[Corion]: LanX: The "good" approach here would be to use the appropriate DBI parameters to make the driver decode strings properly. But that will have a ripple-on effect of messing up all the places where manual decoding happens ;)
[LanX]: which means albeit being broken UTF8 it'll be handled correctly
[LanX]: and the problem only occurs since we changed the emails to base64
[LanX]: my main problem will be to cnvince my colleagues that our productive code is broken oO ... so in the end I will just make a workaround :-/
LanX hates UTF8 for causing knots in his brain and stomach
[Corion]: LanX: Yes, that's the main problem - you have lots (and lots) of workarounds in various places and stages of the processing, and to clean that mess up requires action across the complete codebase. And it's almost impossible to do it piece-by-piece

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2017-01-16 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (150 votes). Check out past polls.