Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
"be consistent"
 
PerlMonks  

Re: Print only if pattern matches

by Athanasius (Prior)
on Jan 24, 2013 at 07:00 UTC ( #1015089=note: print w/ replies, xml ) Need Help??


in reply to Print only if pattern matches

The script can be re-written so:

#! perl use strict; use warnings; while (my $line = <DATA>) { if ($line =~ / ^ \d+ \| (\d+?) \| /x && $1 == 1 && $line =~ s{ ^ (.*\|)* # $1 .*ABC\.pi= ([\d.]+|[\w.]+)* # $2 .*ABC\.id= (\d+|[\w.]+) # $3 .* $ } {$1$2|$3}sx) { print "$1$2|$3\n"; } } __DATA__ ...

While this “works”, it is dubious: the * quantifier in a regex means match zero or more of the preceeding; in the substitution, do you really want to match zero occurrences of (.*\|) or ([\d.]+|[\w.]+)? If not, use the + quantifier meaning one or more.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Comment on Re: Print only if pattern matches
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-04-21 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls