shaludr has asked for the wisdom of the Perl Monks concerning the following question:

open (FILE,"abc345.txt") or die $!; while (<FILE>){ $_ = /((^Query = .*)|(^\>.*)|(Length = .*))/i; print $1 "\n"; print $2 "\n"; print $3 "\n"; print $4 "\n"; } close (FILE) ;
I want the result in new lines. can someone help with that ? but when i print it with new line i get an error Modification of a read-only value attempted at - line 5, <FILE> line 1. Any ideas how to fix this ?

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

Replies are listed 'Best First'.
Re: add new lines to output.
by kennethk (Abbot) on May 06, 2010 at 22:38 UTC
    I'm thinking that line 5 is print $1   "\n";. You skipped a comma there, so Perl interprets that as printing to a filehandle in $1. You likely mean:

    open (FILE,"abc345.txt") or die $!; while (<FILE>){ $_ = /((^Query = .*)|(^\>.*)|(Length = .*))/i; print "$1\n"; print "$2\n"; print "$3\n"; print "$4\n"; } close (FILE) ;

    or

    open (FILE,"abc345.txt") or die $!; while (<FILE>){ $_ = /((^Query = .*)|(^\>.*)|(Length = .*))/i; print $1, "\n"; print $2, "\n"; print $3, "\n"; print $4, "\n"; } close (FILE) ;

    See print for info on proper syntax.

Re: add new lines to output.
by elTriberium (Friar) on May 06, 2010 at 23:56 UTC

    Your code is also missing the match operator. This snippet:

    $_ =

    should be changed to:

    $_ =~

    Otherwise you're trying to assign a value to the $_ variable.

      I did whatever you guys said but it is not working, the error is still the same
        Suggestions above do work just fine. I made one small addition, if your are on Perl 5.10 you can use the // operator. This sets a default value of "no match" as shown below if say $4 is undefined. There aren't any write statements below so you shouldn't get your reported error anymore. You will however start getting print of undefined value warnings which the "//" avoids.

        #!/usr/bin/perl -w use strict; open (FILE,"abc345.txt") or die $!; while (<FILE>){ $_ =~ /((^Query = .*)|(^\>.*)|(Length = .*))/i; print $1 // "no match", "\n"; print $2 // "no match", "\n"; print $3 // "no match", "\n"; print $4 // "no match", "\n"; } close (FILE) ;
        For data line of: Query = 345, prints:
        Query = 345 Query = 345 no match no match
        Update: Had wrong file handle above in "while (<FILE>)". I used <DATA> to test my code and that didn't get corrected in the original post. Anyway the above code does run on my AS Perl 5.10.X machine. I had thought that perhaps something weird was happening with the "=" as that does have significance in a regex, but only in combination with for example "?=". Result is that you do not have to "escape" the = sign. Above code is fine. An escape "\" is required for these characters:  {}[]()^$.|*+?\
        Then I suggest you post your corrected program (with line numbers, so we can see what line the error refers to).

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: add new lines to output.
by pavunkumar (Scribe) on May 07, 2010 at 08:33 UTC
    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 ..... }
      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.

      A reply falls below the community's threshold of quality. You may see it by logging in.