User Control Panel
Advertisements

HELP US, HELP YOU!

problem with open and split.

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Code Review
View unanswered posts
Author Message
kings_on_steeds
Senior Member
Senior Member


Joined: 05 Apr 2004
Posts: 290
Location: Bookham, Surrey, UK
Reputation: 35.1Reputation: 35.1Reputation: 35.1Reputation: 35.1

PostPosted: Tue Mar 22, 2005 12:14 am    Post subject: Reply with quote

ok well i have a peice of code, cer wrote years ago, that i am useing to open a file and go through each record and display it on msn, but it dose loads, and mixes them up

txt file says

Quote:
username=matt@hotmail.com
msg=hello alan

username=alan@hotmail.com
msg=lalausername:


msn says
Quote:
matt@hotmail.com


matt@hotmail.com
hello alan

matt@hotmail.com
hello alan

alan@hotmail.com
hello alan

alan@hotmail.com
lala

alan@hotmail.com
lala

alan@hotmail.com
lala


and my code is

Code:
<br />{<br />  <br />  open (DATA, "./Stores/Msgs.txt");<br />  @data = <DATA>;<br />  close (DATA);<br />  chomp @data;  <br />  foreach $line (@data) {<br />    ($what,$is) = split(/=/, $line, 2);<br />    $what = lc($what);<br />    $what =~ s/ //g;<br />  $variable++;<br />    $hash{$what} = $is;<br />  $username = $hash{username};<br />  $msg = $hash{msg};<br />  $self->sendMessage("$username\n$msg\n");  <br /><br />   }


please help me, if you can!
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Tue Mar 22, 2005 12:47 am    Post subject: Reply with quote

I think what your problem is:

Quote:
{

open (DATA, "./Stores/Msgs.txt");
@data = ;
close (DATA);
chomp @data; 
foreach $line (@data) {
   ($what,$is) = split(/=/, $line, 2);
   $what = lc($what);
   $what =~ s/ //g;
$variable++;
   $hash{$what} = $is;
$username = $hash{username};
$msg = $hash{msg};

$self->sendMessage("$username\n$msg\n"); 

}


If I'm not mistaken, you are setting both $username and $msg to the values in $hash, on the foreach $line loop. So, say it gets to the first line and finds $username - but it doesn't have a $msg yet. So $msg is blank, giving you just matt@hotmail.com

And then, I don't get why it repeats it so many times. But further down it associates alan@hotmail.com with the message from matt@hotmail.com - that's because when it gets to alan@hotmail.com for the first time, it hasn't found the new message (and it's the same problem as before, except for the message isn't blank this time). So that causes a mismatch. On the next line, it starts getting the correct $msg.

And then I don't know why it does that so much too. Maybe there's an outside loop in part of your code you didn't show us.

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
kings_on_steeds
Senior Member
Senior Member


Joined: 05 Apr 2004
Posts: 290
Location: Bookham, Surrey, UK
Reputation: 35.1Reputation: 35.1Reputation: 35.1Reputation: 35.1

PostPosted: Tue Mar 22, 2005 1:02 am    Post subject: Reply with quote

Code:
<br />use lib "./lib";<br /><br />use MSN;<br /><br /><br />open (DATA, "./settings.data");<br />@data = <DATA>;<br />close (DATA);<br />chomp @data;<br /><br />foreach $line (@data) {<br />  ($what,$is) = split(/=/, $line, 2);<br />  $what = lc($what);<br />  $what =~ s/ //g;<br /><br />  $hash{$what} = $is;<br />}<br />$pass = $hash{password};<br />$user = $hash{email};<br /><br /><br />## MODIFY THESE FOR YOUR BOT ##<br />my $handle = $user;<br />my $password = $pass;<br />my $admin = 'nutbar2002@hotmail.com';<br />my $name = "Molms: $handle is Offline To Leave Them A msg Type !leave";<br />my $welcome = $hash{Welcome};<br /><br /># create an MSN object showing all server errors and other errors<br />my $msn = new MSN( 'Handle' => $handle, 'Password' => $password );<br /><br /># OR create an MSN object with all error messages turned off<br />#my $msn = new MSN( 'Handle' => $handle, 'Password' => $password, 'ServerError' => 0, 'Error' => 0 );<br /><br /># OR create an MSN object with full debugging info<br />#my $msn = new MSN( 'Handle' => $handle, 'Password' => $password, 'AutoloadError' => 1, 'Debug' => 1, 'ShowTX' => 1, 'ShowRX' => 1 );<br /><br /># example of setting client info<br />$msn->setClientInfo( 'Client' => 'MSNC2' );<br /><br /># example of setting client capabilites (caps)<br />$msn->setClientCaps( 'Client-Name' => 'MSN Bot/1.0', 'Chat-Logging' => 'Y', 'Client-Template' => 'None' );<br /><br /># example of setting the default message style and P4 name<br />$msn->setMessageStyle( 'Effect' => 'B', 'Color' => '000000', 'Name' => $name );<br /><br /><br /># set handlers<br />$msn->setHandler( 'Connected' => \&Connected );<br />$msn->setHandler( 'Message' => \&Message );<br /><br /><br /># connect to the server<br />$msn->connect();<br /><br /><br /># run the bot<br />my $run = 1;<br />while( $run )<br />{<br />   $msn->do_one_loop();<br />}<br /><br /><br />################################################<br /># handlers<br />################################################<br /><br /><br />sub consoleprint{<br />  print( "You Have One New Message\n" );<br />  sleep (2);<br />  open (DATA, "./Stores/Msgs.txt");<br />  @data = <DATA>;<br />  close (DATA);<br />  chomp @data;<br />  <br />  foreach $line (@data) {<br />    ($what,$is) = split(/=/, $line, 2);<br />    $what = lc($what);<br />    $what =~ s/ //g;<br />  $variable++;<br />    $hash{$what} = $is;<br />  <br />}<br />}<br />sub Connected<br />{<br />   my $self = shift;<br /><br />   print( "Connected\n" );<br /><br />   # example of a call with style and P4 name<br />   $msn->call( $admin, "I am connected!", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => '$name' );<br />   $self->setName($name);<br />}<br /><br />sub Message<br />{<br />   my( $self, $username, $name, $message, %style ) = @_;<br /><br />   if( $message =~ /^!exit$/i )           # exits the bot<br />   {<br />  $msn->setName("Nutbar 2002@hotmail.com, is back online");<br />  $msn->disconnect( );<br />  $run = 0;   <br />   }<br />   elsif( $message =~ /^!broadcast\s+(.*)$/i )  # sends a broadcast message<br />   {<br />  # example of a broadcast with style and P4 name<br />  $reply = $msn->broadcast( $1, 'Effect' => 'BI', 'Color' => '0000FF', 'Name' => 'Broadcaster' );<br />   }<br />   elsif($message =~ /^!leave$/i ){<br />  <br />  $self->sendMessage("$Welcome\n\n$username\nTo leave a message for $handle Type !msg followed by the message you would like to leave\nAnd they will get back to you as soon as they can.\nFor MoRe Info About MOLMS type !about\n\n Thank You");<br />}<br />   elsif($message =~ /^!msg\s+(.*)$/i  ){<br />   open (FILE, '>> ./Stores/Msgs.txt');<br />   print FILE "username: $username";<br />   print FILE "\n";<br />   print FILE  $message;<br />   print FILE "\n\n";<br />   close FILE;<br />   my $messages  +1;<br />   $self->sendMessage("Thank you for your message, I Will Reply When i Come Back Online");<br />   &consoleprint;<br />   $msn->broadcast( "Alan $username Has Left A Msg", 'Effect' => 'BI', 'Color' => '0000FF', 'Name' => 'Broadcaster' );<br />   <br />}<br />   elsif( $message =~ /^!about$/i )           # exits the bot<br />   {<br />  $self->sendMessage("About me Goes Here");<br />   }<br />   elsif( $message =~ /^!password Admin123$/i  )           <br />   {<br />  <br />  open (DATA, "./Stores/Msgs.txt");<br />  @data = <DATA>;<br />  close (DATA);<br />  $ones = @data;<br />  chomp @data;  <br />  foreach $line (@data) {<br />    ($what,$is) = split(/=/, $line, 2);<br />    $what = lc($what);<br />    $what =~ s/ //g;<br />  $variable++;<br />    $hash{$what} = $is;<br />  $username = $hash{username};<br />  $msg = $hash{msg};<br />  $self->sendMessage("$username\n$msg\n");  <br /><br />   }}<br />   else                                         # repeats what the user said<br />   {<br />  $self->sendMessage( "I Am Offline Right Now To Leave A Message Type !leave And Follow The Instructions, Thanks\n Alan." , %style );<br /><br />   } <br />}<br /><br />


thats the whole code, i have looked and i dont, no, all it is, is the echo bot with a few added elsifs in the message handler.
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Tue Mar 22, 2005 3:43 pm    Post subject: Reply with quote

QUOTE(kings_on_steeds @ Mar 21 2005, 04:14 PM)
please help me, if you can!
[right][snapback]47128[/snapback][/right]


In the future at least ask a question. Why in the world would be read your code and exam the output in order to figure out what you think it is doing wrong?

You told it to send a message for each line. Its doing that. The message you send is two lines long and is composed info store in the hash. I know what you realy want to do, and i even know how to do it. If you want the solution you will have to figure out what your question is and ask it though.

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Code Review All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group