Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Regular Expression tricky newline problem

by Aristotle (Chancellor)
on Jan 03, 2006 at 13:16 UTC ( #520584=note: print w/ replies, xml ) Need Help??


in reply to Re: Regular Expression tricky newline problem
in thread Regular Expression tricky newline problem

Careful, youíre invoking grep in scalar context. $keeper will only contain the count of matches. This has to be written with a parenthesised my, like so:

my ( $keeper ) = grep /^Line 3 : /, @lines;

However, that always goes through the entire data, regardless of where the match is found. A better way would be List::Utilís first; with which the context does not matter either:

use List::Util qw( first ); my $keeper = first { /^Line 3 : / } @lines;

Makeshifts last the longest.


Comment on Re^2: Regular Expression tricky newline problem
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2015-07-07 16:17 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 (91 votes), past polls