anchorite has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,
I have some faulty code:
while ($line=<INFILE>) { chomp $line; if ($line=~/^COMPND[\s]{3}3/) { $line=join(' ',split(' ',$line)); chop $line; $chain=substr($line,length($line)-1,1); $lastchain=$chain; print OUTFILE ("$chain\n"); } else { $lastchain='@'; } }
What it tries to do is go through a file, find a pattern, store the pattern and perform a manipulation on it (ending up in $lastchain). However I want the loop to store $lastchain as '@' if the pattern is never found. The else statement is not working for me for obvious reasons, any suggestions O wise ones?
Yours in humility, anchorite.

Replies are listed 'Best First'.
Re: otherwise condition
by dragonchild (Archbishop) on Aug 25, 2003 at 13:21 UTC
    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.

      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.

Re: otherwise condition
by bobn (Chaplain) on Aug 25, 2003 at 13:30 UTC

    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.

      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.