Posted: Fri Jul 01, 2005 12:32 pm Post subject: If Else Statement Problem
I'm trying to make a bot from pretty much scratch. I'm editing the on_im.pl handler to try and have my bot read from a .txt file something that looks like this:
Hi:Hi $client
How are you:I am fine, thanks for asking
Did you have a nice day:I have had a nice day so far
Then, the bot would split it at the colon into $input and $output. It would compare $msg to $input. If they match, it sets $output as $reply, and later returns $reply. I also have an Else statement in there to catch any input that is not on the .txt file. The else statement does about the same thing. Except, instead of setting $output as $reply, it sets a predetermined message as $reply. My problem is that my bot will ONLY return the else statement reply, not the input output reply. Here is on_im.pl:
Code:
sub on_im {
# Get variables from the server.
my ($aim,$client,$msg,$away) = @_;
my $screenname = $aim->screenname();
my $time = localtime();
if (!-d "./logs/$client") {
mkdir ("./logs/$client");
}
# Make sure this wasn't an "away message"
if ($away == 0) {
# Filter the message for HTML.
$msg =~ s/<(.|\n)+?>//g;
print "$time\n"
. "[$client] $msg\n";
#Open Client's Log File
open(FILE,">>./logs/$client/log.txt");
#Add on to the end the time and their message
print FILE "$time\n$msg\n\n";
#Close Log File
close(FILE);
open (REPLYDAT, "./ifmsgisthen.txt");
@replydat = <REPLYDAT>;
close (REPLYDAT);
foreach $replydatline (@replydat) {
($input,$output) = split(/:/, $replydatline, 2);
}
if ($msg eq "$input") {
$reply = "$output";
}
else {
$reply = "That is nice";
}
Try something like this also note you do not need to quite variables..
Code:
sub on_im {
my ($aim,$client,$msg,$away) = @_;
my $screenname = $aim->screenname();
my $time = localtime();
if (!-d "./logs/$client") {
mkdir ("./logs/$client");
}
# Make sure this wasn't an "away message"
if ($away == 0) {
# Filter the message for HTML.
$msg =~ s/<(.|\n)+?>//g;
print "$time\n"
. "[$client] $msg\n";
# Open Client's Log File (Read, Write and Close it)
Only use quotes when you need to put other things in the string besides just a variable.
___________________
Also, your problem is here:
Code:
if ($msg eq "$input") {
$reply = "$output";
}
else {
$reply = "That is nice";
}
You are checking if/else for ALL inputs.
For example, if your reply dealy looks like this:
Quote:
hello:Hello human.
what's up:Not much you?
If the human says "hello".....
Quote:
if $msg eq $input ("hello")...
Condition is true: setting $reply to $output...
next...
if $msg eq $input ("what's up")...
Condition is false: setting $reply to "That is nice"
next...
See what the problem is? For each input, if the message doesn't match it sets $reply to the no-reply thing.
You need to instead just use the If statement, and when it's done trying replies, see if $reply has a value...
Code:
if (length $reply == 0) {
# no reply has been given yet
$reply = "That is nice";
}
That didn't work, so I tried adding some print statements in. Here is on_im.pl:
Code:
sub on_im {
# Get variables from the server.
my ($aim,$client,$msg,$away) = @_;
my $screenname = $aim->screenname();
my $time = localtime();
if (!-d "./logs/$client") {
mkdir ("./logs/$client");
}
# Make sure this wasn't an "away message"
if ($away == 0) {
# Filter the message for HTML.
$msg =~ s/<(.|\n)+?>//g;
print "$time\n"
. "[$client] $msg\n";
#Open Client's Log File
open(FILE,">>./clients/$client/log.txt");
#Add on to the end the time and their message
print FILE "$time\n$msg\n\n";
#Close Log File
close(FILE);
open (REPLYDAT, "./ifmsgisthen.txt");
@replydat = <REPLYDAT>;
close (REPLYDAT);
foreach $replydatline (@replydat) {
print "$replydatline";
($input,$output) = split(/:/, $replydatline, 2);
}
if ($msg eq "$input") {
print "msg is equal to input";
$reply = $output;
last;
}
else {
print "using else statement";
$reply = "That is nice";
}
Joined: 03 May 2006 Posts: 2292 Location: Colorado
Posted: Mon Jul 04, 2005 11:12 pm Post subject:
Instead of printing "$replydatline" you should try printing out what you split apart. Also print what you are comparing before the if line. Something like print "'$msg' eq '$input;" might be very informative. _________________ Eric256
Proud previous owner and current admin of Bot-depot.com