Jerry,

Jerry> What am I doing wrong?

Such an open question. I assume you mean what is wrong with your code. To
answer, it isn't doing what you want it to do :-)

Jerry> I am trying to setup a single regex
Jerry> to breakdown the following lines:
Jerry> 
Jerry> Jerry    2.7    4      4.5            mon
Jerry> Mark    -14    -10.75 -10            new
Jerry> 
Jerry> 
Jerry> /(\w+)\s+(-?\d+.\d+)\s+(-?\d+.\d+)\s+(-?\d+.\d+)\s+(\w+)/;

At first blush, you're going to run into problems when the number you're
trying to capture is a whole number without a decimal place holder (#.00).
Take a look at your expression, `(-?\d+.\d+)` in row-1 column-3 you have a
value of `4`. That won't get captured because it isn't two numbers split by
any value. Additionally, you're telling Perl to look for a number after the
any-character. So `4`, `44`, `44.`, or `44324242` won't work. I would
suggest the following:

    # UN-TESTED
    /(\w+)\s+(-?\d+.?\d*)\s+(-?\d+.?\d*)\s+(-?\d+.?\d*)\s+(\w+)/
    
This tells Perl the decimal is optional. It also says the values after the
decimal are optional too (this is important if there is no decimal). This
syntax is fairly noisy. As an alternative, try using split() Something along
the lines of:

#! /usr/bin/perl
use strict;

# UN-TESTED
my ($value, @parsed);
$value = "adam  1.2     2.2     3.2     wed";
@parsed = split(/\s+/, $value);
print "$_\n" for (@parsed);

Unless the number of columns will vary by record this should work nicely.

Regards,
Adam



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to