in reply to otherwise condition

while ($line=<INFILE>) { chomp $line; if ($line=~/^COMPND[\s]{3}3/) { $line=join(' ',split(' ',$line)); # 1 chop $line; # 2 $chain=substr($line,length($line)-1,1); # 3 $lastchain=$chain; # 4 print OUTFILE ("$chain\n"); } else { $lastchain='@'; # 5 } }

Okaaaay. I suspect that lines 1-5 are not doing what you think they're doing.

  1. This splits on a single space, then joins on a single space ... giving you back the same line. I suspect you were looking for $line = join ' ', split /\s+/, $line; instead.
    Update: gmax has kindly informed me that this is a special case. I would still recommend splitting on /\s+/ instead, as it makes your intent obvious. (And special cases have a habit of unspecialing themselves in later releases ...)
  2. Why are you chopping the line again? You've already chomped it ...
  3. This line gives you the last character. If that's what you're looking for, then $chain = substr $line, -1; would work better, and faster.
  4. Why are you creating $chain just to assign it to $lastchain and print it out? Do you mean to concatenate instead? (That's .=, not =)
  5. And, like #4, I suspect to meant to concatenate, not assign.

The else statement looks just fine, to me.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: otherwise condition
by bobn (Chaplain) on Aug 25, 2003 at 13:55 UTC

    The else statement doesn't do what the OP asked, which is to set $lastchain = '@' if NO lines are matched. As written, if every line but the last matches, $lastchanin will still be set to '@'.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.