Hey...I was reading Eric's Tutorial on Hashes, and well I thought I'd try to write something that involved real hashes. But naturally, I fail.
The following is a command that "turns on" other commands. What it does, is it opens up a text file called onoff.txt, and runs down the list of commands. When I type !on sendim (for example), it will search through the file, and its supposed to change the numerical value from 0 to 1. However, the way it works now, if I'm lucky, is it just erases the entire onoff.txt file.
Code:
######################################<br /># #<br /># ## #<br /># ########### # # #<br /># # # # #<br /># # ### # ### #<br /># # # # # #<br /># # ### # #<br /># # # # # #<br /># # ########## #<br /># # #<br /># ########### #<br /># #<br /># @-Squared Productions #<br /># #<br />######################################<br /><br />sub on {<br /><br /> my ($victim,$msg,$aim) = (shift,shift,shift);<br /> my $sn = $screenname;<br /> my $toprint;<br /> my %onoff;<br /> my @new_comm;<br /> my $new;<br /><br /> $msg =~ s/(\!|\.|\/)on //ig;<br /><br /> my $adminonly = 1;<br /><br /> open (FILE, "commands/admin.txt");<br /> my @admin = <FILE>;<br /> close(FILE);<br /> chomp @admin;<br /><br /> my $is_admin = 0;<br /><br /> foreach my $admin (@admin) {<br /> if ($victim eq $admin) {<br /> $is_admin = 1;<br /> break;<br /> }<br /> } <br /><br /> open (COMM, "texts/onoff.txt");<br /> my @existing_comm = <COMM>;<br /> close (COMM);<br /> chomp(@existing_comm);<br /><br /> my $final; <br /><br /> foreach my $line (@existing_comm) {<br /> my ($comms,$val) = split(/\]\[/, $line);<br /> $onoff->{$comms} = "$val";<br /> $toprint .= "Current line...$comms ($onoff->{$comms})\n";<br /> }<br /><br /> if ($adminonly == 1 && $is_admin == 1) {<br /> my $com = $msg;<br /> if ($onoff->{$com} == 0) {<br /> $onoff->{$com} = "1";<br /> $toprint .= "I have turned on command $com\n";<br /> $reply = "I have turned on command $com."; <br /> } else {<br /> $reply = "$com is already on!";<br /> }<br /> } else {<br /> $reply = "Sorry, this is an admin only command";<br /> }<br /><br /> @new_comm = sort keys %onoff;<br /> chomp(@new_comm);<br /> open (NEW, ">texts/onoff.txt");<br /> foreach $new (@new_comm) {<br /> print NEW "$new\]\[$onoff->{$new}\n";<br /> }<br /> close (NEW);<br /><br /> &print($toprint);<br /> return $reply;<br /><br />}1;
Example of onoff.txt
Quote:
sendim][1 blackjack][1 tictactoe][1
I know this isn't a great way to use hashes...or probably not even a "real way". However, I wanted to use "hashes" so I could more easily change the values (0/1). Another way I wrote the command used simple "teach" manipulation:
Code:
######################################<br /># #<br /># ## #<br /># ########### # # #<br /># # # # #<br /># # ### # ### #<br /># # # # # #<br /># # ### # #<br /># # # # # #<br /># # ########## #<br /># # #<br /># ########### #<br /># #<br /># @-Squared Productions #<br /># #<br />######################################<br /><br />sub on {<br /><br /> my ($victim,$msg,$aim) = (shift,shift,shift);<br /> my $sn = $screenname;<br /> my $toprint;<br /> my $found = 0;<br /><br /> $msg =~ s/(\!|\.|\/)on //ig;<br /><br /> my $adminonly = 1;<br /><br /> open (FILE, "commands/admin.txt");<br /> my @admin = <FILE>;<br /> close(FILE);<br /> chomp @admin;<br /><br /> my $is_admin = 0;<br /><br /> foreach my $admin (@admin) {<br /> if ($victim eq $admin) {<br /> $is_admin = 1;<br /> break;<br /> }<br /> } <br /><br /> if ($adminonly == 1 && $is_admin == 1) {<br /> my $com = $msg;<br /><br /> open (COMM, "texts/onoff.txt");<br /> my @existing_comm = <COMM>;<br /> close (COMM);<br /> chomp(@existing_comm);<br /><br /> my $final; <br /><br /> foreach my $line (@existing_comm) {<br /> chomp($line);<br /> my ($comms,$val) = split(/\]\[/, $line);<br /> $toprint .= "Current line...$comms\n";<br /> if ($comms =~ /$com/i) {<br /> if ($val == 0) {<br /> $val = 1;<br /> $line = "$comms\]\[$val";<br /> $reply = "I have turned on command $comms.";<br /> } else {<br /> $reply = "$comms is already on!";<br /> }<br /> } else {<br /> $reply = "$com does not exist, or is not listed";<br /> }<br /> $final .= "$line\n";<br /> }<br /><br /> open (NEW, ">texts/onoff.txt");<br /> print NEW "$final";<br /> close (NEW);<br /><br /> } else {<br /> $reply = "Sorry, this is an admin only command";<br /> }<br /><br /> &print($toprint);<br /> return $reply;<br /><br />}<br />1;
...but I'm trying to move away from this method. _________________ Anthony Arslan
@-Squared Enterprises
MacroHard Corporation
I still cannot get the hash version of onoff() function to work, however, I'm making do with the second version.
Also, here is my command.pl which checks if the user is trying to use a command. I had previously posted this with my modification, so it would check subs instead of files. However, since then I have modified it to use the onoff() function.
Code:
######################################<br /># #<br /># ## #<br /># ########### # # #<br /># # # # #<br /># # ### # ### #<br /># # # # # #<br /># # ### # #<br /># # # # # #<br /># # ########## #<br /># # #<br /># ########### #<br /># #<br /># @-Squared Productions #<br /># #<br />######################################<br /><br />sub commands {<br /><br /> #Retrieves the victim and the message.<br /> my ($victim,$msg,$aim) = (shift,shift,shift);<br /> my $reply;<br /><br /> #Resets the default values.<br /> my $command = 0;<br /><br /><br /> @mess = split(/ /,$msg);<br /> $mess[0] =~ s/(\/|\!|\.)//ig;<br /> if (exists &{$mess[0]}) {<br /> open(FILE, "texts/onoff.txt");<br /> my @activity = <FILE>;<br /> close(FILE);<br /> chomp(@activity);<br /> foreach my $active (@activity) {<br /> my ($what,$how) = split(/\]\[/,$active);<br /> if ($what eq $mess[0] && $how == 1) {<br /> $reply = &{$mess[0]}($victim,$msg,$aim);<br /> $command = 1;<br /> } elsif ($what eq $mess[0] && $how == 0) {<br /> $reply = "Sorry, but the command was turned off by the admin.";<br /> $command = 1;<br /> }<br /> }<br /> } else {<br /> $reply = "This was not a command.";<br /> }<br /><br /> return $command,$reply;<br />}<br />1;
EDITED: What's also a good thing about this additional feature, if you use my version of command.pl is that there used to be concern about people trying to call subs that weren't commands. For Example: When I first posted the new command.pl, Eric said that people could say
Quote:
!commands
...and the bot would try to open the sub command() and the bot would crash because of a loop. However, if you use the onoff addition, the commands won't be caleld unless you list them in the onoff.txt _________________ Anthony Arslan
@-Squared Enterprises
MacroHard Corporation
Thanks....but I could really use the help with hashes. I've bought a book on Perl, but it doesn't seem to help me lol :wacko: _________________ Anthony Arslan
@-Squared Enterprises
MacroHard Corporation
I'm having an issue with the second form of my on() function. (The form without the hashes.)
It keeps giving me back the reply
Quote:
$com does not exist, or is not listed
I know why it does it...its because after it changes the value for the required command in the onoff.txt file, it continues through the rest of the commands. I just can't figure out a way around it though, so that it tells me that it properly turned off the command I asked. _________________ Anthony Arslan
@-Squared Enterprises
MacroHard Corporation