Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://520584]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2017-08-23 02:53 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (345 votes). Check out past polls.