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.
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 ...)
- Why are you chopping the line again? You've already chomped it ...
- 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.
- Why are you creating $chain just to assign it to $lastchain and print it out? Do you mean to concatenate instead? (That's .=, not =)
- 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. | [reply] [d/l] [select] |
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.comAll code given here is UNTESTED unless otherwise stated.
| [reply] |
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.comAll code given here is UNTESTED unless otherwise stated.
| [reply] [d/l] |
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.
| [reply] |
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.
| [reply] [d/l] |