in reply to otherwise condition

You are re-using too many variables, $lastchain in particular.

$found = 0; while ($line=<INFILE>) { chomp $line; if ($line=~/^COMPND[\s]{3}3/) { $found = 1; # other processing here; } } $lastchain = '@' unless $found;

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

All code given here is UNTESTED unless otherwise stated.

Replies are listed 'Best First'.
Re: Re: otherwise condition
by anchorite (Initiate) on Aug 25, 2003 at 14:02 UTC
    Dear respondees,
    bobn's code is exactly what I was looking for thankyou.
    Sorry to confuse people with the code in my while loop, it does exactly what I want it to do although it has correctly been pointed out that it is messy. In response to a specific query, the chop is used to get rid of a semicolon that is always at the end of the pattern.
    The else part is unfaulty code but does not do what I want it to do.
    Many thanks.
      the chop is used to get rid of a semicolon that is always at the end of the pattern.

      I would recommend either commenting that it is removing a semi-colon or using a pattern match to get rid of it, along the lines of $line =~ s/;$//;. Unless this is a script which has to execute in a specific amount of time, self-commenting code is much better than fast code.

      ------
      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.

        Or just drop the chop altogether and use
        $chain=substr($line,length($line)-2,1);
        instead of
        $chain=substr($line,length($line)-1,1);