Gossamer Forum
Home : General : Perl Programming :

Chomp lines in a file

Quote Reply
Chomp lines in a file
Okay, I do not know why this will not work, but maybe you all can help. I have list of bots IP # and Addr. It's stored in a file called engines.list and there is a new line feed at the end of each address or IP #.

What I am trying to do is to match the env host and addr variables with each line in the file. If it successfully finds a match, it executes another subroutine, otherwise another subroutine executes.

Here's the code that I am constantly looking at and can't get it to figure out. I have tested the script by adding my IP to the list and it only works if I add my number to the bottom of the list. If I add it to the top or in the middle of the list, the script doesn't find my number.

Code:
$RH = "$ENV{'REMOTE_HOST'}";
$RA = "$ENV{'REMOTE_ADDR'}";

open( INF, "engine.list" );
@enginelist = <INF>;
close INF;

&detect;

if ($isRobot eq 1) { &robot; } else { &normal; }

exit;

sub detect{
$isRobot = 0;
foreach $line (@enginelist) {
chomp $line;
if (($RA eq $line) or ($RH eq $line)) { $isRobot = 1; }
}
}

Does the code look right? I keep getting isRobot variable 0 even though my IP number is listed somewhere in the engine.list, unless it is at the very bottom of the list. Then the variable is 1.
Quote Reply
Re: Chomp lines in a file In reply to
Here's how I would do that:

Code:
$RH = "$ENV{'REMOTE_HOST'}";
$RA = "$ENV{'REMOTE_ADDR'}";
open( INF, "engine.list" );
@enginelist = <INF>;
close INF;
&detect;
if ($isRobot eq 1) { &robot; } else { &normal; }
exit;
sub detect{
$isRobot = 0;
LINE: foreach $line (@enginelist) {
chomp $line;
if (($RA eq $line) or ($RH eq $line)) {
$isRobot = 1;
last LINE;
}
}
}

Once you have a match, you need to exit the subroutine, which your code was not doing. Instead it just kept procecessing the code.

I hope this helps.