Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: add new lines to output.

by pavunkumar (Scribe)
on May 07, 2010 at 08:33 UTC ( #838890=note: print w/ replies, xml ) Need Help??


in reply to add new lines to output.

I think You have missed the patten matching operator. 
And with out checking pattern matching status , you are 
seeking for $1,$2,$3,$4 variables. If the matches get 
succeeded only then you can access those variables. 

So you check with if condition 
if ( $_ =~ ( regulare expression ) { # print the $1,$2,$3,$4 ..... }


Comment on Re: add new lines to output.
Download Code
Re^2: add new lines to output.
by Marshall (Prior) on May 07, 2010 at 09:23 UTC
    This is not the problem. The Op's regex guarantees that something of $1,$2,$3,$4 will be undefined even if the true/false sense of a "match" succeeds.
    For example, a line can only start with one thing. e.g, $2 and $3 cannot both be "defined" at the same time... A line can start with one of these 2 things or something else. "Query = " ">something"
    The Ops "Length = .*" term will never match unless the line didn't start with Query or > because the .* in those terms will "greedily" gobble up all the characters past those terms in the line. In that case $2 and $3 will be undefined. This is actually ok if that is what was intended.

      open (FILE,"abc.txt") or die $!; while (<FILE>){ ($_ =~ /((^Question = .*)|(^\>.*)|(Height = .*)|(Score = \d) +)/i); print $1 "\n"; print $2 "\n"; print $3 "\n"; print $4 "\n"; } close (FILE) ;
      error : Modification of a read-only value attempted at - line 4, <FILE> line 1. ALSO,

      can anybody tell me how to get just the first 4 matches for all the regex too ?

        Why aren't you paying attention to what kennethk,elTriberium and myself have previously said? You have gotten "rock solid" advice about your "Modification of a read-only value attempted at - line 4" problem". Why do you refuse to take that advice and then have the nerve to ask another question?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2015-07-05 03:58 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 (60 votes), past polls