Personally I wonder at what you are doing. If your file contains only records in the above format then use split as other monks have suggested. However if you are trying to extract lines that match from a bunch of other crud then the regex will have to be the way to go.
Also your regex for floating point numbers leaves quite a bit to be desired. For instance it will match ip addresses as well as floating point values (not to mention things like "................."). There are a variety of regexes that will handle numbers like this correctly to be found in the FAQS.
my $num_rex=qr/(-?(?:\d+(?:\.\d*)?|\.\d+))/; # modified from: perldoc
+-q scalar is a number
while (<DATA>) {
if (/^\s* (\w+) \s+ $num_rex \s+ $num_rex (?: \s+ $num_rex \s+ $num_
+rex )? \s*$/x) {
print "Matched a word and ",(defined $4?"four":"two")," numbers:
+$1 $2 $3",(defined $4?" $4 $5\n":"\n");
}
}
__DATA__
Abc 21223.7 21225.33 22270.3 22280.1
Def 21600.23 24567.43
Oh, I changed it to be tolerant of leading and trailing whitespace. YMMV.
HTH
--- demerphq
my friends call me, usually because I'm late....
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.