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:
{}[]()^$.|*+?\
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.