Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Regular Expressions question

by Lotus1 (Chaplain)
on Oct 30, 2012 at 03:24 UTC ( #1001449=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Regular Expressions question
in thread Regular Expressions question

my @check = grep { chomp $_; $_ } <DATA>;

Map should be used for this not grep. Consider the following.

use warnings; use strict; my @check = grep { chomp $_; $_ } <DATA>; print ">>@check<<" __DATA__ line1 0 line3

The output is:

>>line1 line3<<

Grep looks at the value the block returns and if true it returns $_. Map is used to do something to each element and pass along the result from each.

To me it is clearer and simpler to do this:

my @check = <DATA>; chomp @check;


Comment on Re^3: Regular Expressions question
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2015-07-03 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (56 votes), past polls