Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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 ..... }

Replies are listed 'Best First'.
Re^2: add new lines to output.
by Marshall (Monsignor) 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?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2017-02-22 02:56 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (323 votes). Check out past polls.