User Control Panel
Advertisements
HELP US, HELP YOU!
Author
Message
Nate God Like Joined: 12 Nov 2003Posts: 553
Posted: Thu Jan 01, 2004 7:14 am Post subject:
How would you list like the first level of multilevel keys in a hash? Okay, here's the bit where it creates a hash based off of files in a folder. Code: # Begin loading up the bots.<br />print ":: Loading bot files... ";<br /><br />my %bots;<br /><br />opendir(DIR, "./bots");<br /> foreach $file (sort(grep(!/^\./, readdir(DIR)))) {<br /> #$file =~ s/\.(.*)//g;<br /><br /> open (BOT, "./bots/$file");<br /> my @bot_data = <BOT>;<br /> close (BOT);<br /><br /> foreach $line (@bot_data) {<br /> chomp $line;<br /> $line =~ s/: /:/g;<br /> my ($what,$is) = split(/:/, $line);<br /><br /> $what =~ s/ //g;<br /> $what = lc($what);<br /><br /> if ($what eq "screenname") {<br /> # We have a screenname.<br /> my $screenname = $is;<br /> }<br /> elsif ($what eq "password") {<br /> # Time to set the password.<br /> $bots{$screenname}->{"pw"} = $is;<br /> }<br /> elsif ($what eq "listener") {<br /> # The client it connects to.<br /> $bots{$screenname}->{"listener"} = $is;<br /> }<br /> else {<br /> $bots{$screenname}->{$what} = $is;<br /> }<br /> #}<br /> }<br />}<br />closedir(DIR);<br />print "Done.\n";
And the text file looks something like this: Code: ScreenName: ChaosTesting<br />Password: secret<br />Client: AIM<br />Profile: ./data/aim/profiles/ChaosTesting.html<br />Buddies: ./data/aim/buddies/ChaosTesting.txt
And I've tested printing the hash values and by the time it's done with the above file the hash looks like this: Code: %bots = (<br /> ChaosTesting [<br /> Password => secret,<br /> Client => AIM,<br /> Profile => ./data/aim/profiles/ChaosTesting.html,<br /> Buddies => ./data/aim/buddies/ChaosTesting.txt,<br /> ]<br />);
And it would be kind of like that, so each first level is a bot's screenname or handle, and the second level are the details on it. How would I do a foreach keys loop to get the first level? I tried this: Code: foreach $item (keys %bots) {<br /> print "$item\n";<br />}
Didn't get anything. And then I tried this: Code: @screennames = $bots;<br />foreach $item (@screennames) {<br /> print "$item\n";<br />}
And that didn't get anything either. Anyway, the purpose is so that you can have any amount of bots in your "bots" folder and it will run them all simultaneously using their do_one_loop commands.
Back to top
eric256 The Keymaker Joined: 03 May 2006Posts: 2292 Location: Colorado
Posted: Thu Jan 01, 2004 10:29 am Post subject:
Code: <br />use Data::Dumper;<br />print Dumper(%bots);<br />
And see what you get. Then you can be sure that its reading the file and filling in the hash correctly. _________________ Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top