On Jun 3, M.W. Koskamp said:

>> #sample of the text
>> Nitrogen 0.0  -5.78  0.0  0.0  0.0
>>
>> #sample of the program.
>> while(<COMP2FILE>){
>>
>> if (/(Nitrogen) *([0-9.\-]*) *([0-9.\-]*) *([0-9.\-]*) *([0-9.\-]*)
>> *([0-9.\-]*)
>> /) {
>> if ($2 <= 0.0) {
>> $name = $1; $nitrogen1 = $2; $nitrogen2 =$3; $nitrogen3 = $4; $nitrogen4 =
>> $5;
>> $nitrogen5 = $6;}}
>> }
>>
>>
>> I thoguht placing the \- would get rid of the problem but it didn't.
>> So, the end result is that $nitrogen1 = 0.0 and $nitrogen2 = -5.78  0.0
>> 0.0  0.0.  the other variables after that are undefined.
>
>(Nitrogen) will match "Nitrogen"
>\s* will match any number of spaces
>([0-9.\-]*) will match the largest sequence of any character since there is
>a dot in the expression.....

No; a . inside a character class is not special at all.

Brent, I suggest you change your / */ to /\s+/ (which matches any type of
whitespace, not just space), and the /([0-9.\-]*)/ to something a bit more
robust too, like /(-?\d+\.\d+)/, because if you know there'll always be a
decimal point and a number after it, you can be that specific.

Then again, you might just want to use the split() function:

  while (<FILE>) {
    my @fields = split;  # this is like:  split(' ', $_)
    if ($fields[0] eq 'Nitrogen' and $fields[1] <= 0) {
      # do stuff here
    }
  }

It's my guess that some of those spaces are really tabs...

On another note, Mark-Jason Dominus has a talk he gives about "red
flags" -- things in your program that should alert you to probable places
where your program could be improved.  One such flag is the use of
families of variables:

  my ($foo1, $foo2, $foo3, $foo4) = (...);

would be much better written (in 90% of cases) as

  my @foo = (...)

which are then accessed as $foo[0], $foo[1], etc.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **

Reply via email to